A view is a virtual table defined by a query. Once created a view would behave like a table. in simple words, a view is a select statement, saved in a database and given a name. The most important benefit of using a view is limiting access to certain columns and rows and eliminating the need for executing large select statements. In this blog post, I will create a view from one of the tables in the AdventureWorks2012 database. AdventureWorks is a sample Database shipped with SQL Server and can be downloaded from the "Microsoft SQL Server Database Product Samples" website. The first step is to select all the columns from the Customer table in the AdventureWorks Database. As could be observed in the screenshot below 19,820 rows have been returned after doing so. Now we create a view, named CustomerInfo, that selects only a few of the columns above. For the purpose of this example, I have chosen the CustomerID, AccountNumber, and ModifedDate columns for this view. see the screenshot below for the code used to create a view. After successfully creating the view, I selected the data from the view. As could be observed below, the same 19,820 rows have been returned, however; only the three columns defined by the view statement have been displayed. Now that I've demonstrated how a view is created, I want to use a view in order to change the column names displayed in the column header. Perhaps the person who created the table has used column names that you find confusing or don't like to use. Creating a view is one of the best ways for resolving this issue, as it is more convenient than using column aliases, given that a view only needs to be executed once. In order to rename the column names using a view, we will first need to drop the view and then recreate it, with the difference that this time we will define the new column names inside parenthesis next to the view name. see the screenshot below. After successfully creating the new view, we select data from the view. Note the change in column names.
0 Comments
Leave a Reply. |
CategoriesArchives
June 2020
|