We can briefly define View as a virtual table that does not physically store records, and is determined by a query. So, There is no more space need when creating views.
Typically, we create view for complex queries that join multiple tables, to increase readability of our code. Thus, we do not have to write complex queries every time. Let’s clarify the topic by creating a view example. Click New View on the Views tab of a database as follows.
Create a View Using SSMS
Double-click the tables, views, functions, or synonyms that we want to add to the view on the screen.
After selecting the desired objects, we click Close and we select the relevant columns from the objects we selected. Then, the query that will create the View appears as in the following screenshot.
You can save the view with CTRL + S. It’ll ask us what the name of the view will be. We will name it as ViewExample.
When you click OK, you may receive an error as follows. The reason for this error is that the name of the view is the same as the name of one of the tables in the view.
Cannot save a view/funtion/procedure that is self-referenced.
As a workaround, you can save the view with another name.
Create a View Using TSQL
If you want to create a view by using TSQL, you can use the following script.
1 2 3 4 | CREATE VIEW [dbo].[ViewExample] AS SELECT * FROM [dbo].[MyTable] GO |
We can add indexes to the View. Indexed Views are known as the equivalent of Materialized View in Oracle. You can find details in the article “What is Indexed View in SQL Server and How To Create an Indexed View”.