In this study, we explain to you how to create View in SQL, modify, rename, and delete Views in SQL Server.
The SQL Views are the virtual tables, which consist of columns, rows from the referenced table. Unless we defined indexed views, a view in SQL Server does not store a set of values in a database. When you use the SELECT Statement against a view, then the records will come from the table that we referenced while creating a view.
Create View in SQL Server
You can use Transact-SQL Query or SQL Management Studio to create views in SQL Server. Before we get into the example, You can use a Sql Server View for the following purposes:
- To simplify the data as per the user needs.
- Restrict the Users not to access the entire database.
Create View in SQL Server Management Studio
In this example, we will show you the steps involved to create View in SQL Server using the Management Studio. Following are the restrictions we have to remember while creating views, and they are:
- A View can be created only in the current database
- The view allows us to have a maximum of 1024 columns
To see the existing Sql Server Views, Please select the Database that has the views. From the below screenshot, you can observe that our database has no views.
Right-click on the Views folder in Management Studio will open the context menu. To create a view in SQL Server, Please select the New View..option from it.
Once you click on the New View… option, the Query Designer opens in a separate window, and a Pop-up window to add the required tables.
Here, you can add functions, tables, or views. From the below, you can observe that we selected the MyEmployees table and Department table.
Once you selected the required tables, it will show the Query Designer. Please choose the columns needed by check-marking the column names in the Diagram Pane.
In this SQL Server example, we chose the First Name, Last Name, Education, Occupation, Department, Yearly Income, Sales, Hire Date from two tables. Remember, you can Join two tables by dragging one column on to the other.
The sort Type property is used to apply for ORDER BY functionality. Within the Grid Pane, Please change the Sort Type to Ascending or Descending. In this SQL Server views example, we are sorting Yearly Income in the Descending Order.
Once you selected the Descending Operation, it will show you the sorting representation
From the below screenshot, you can see the final select query that we designed
Let us execute this Sql Server views query that we designed using the Management Studio and see the result
-- SQL View Example SELECT TOP (100) PERCENT dbo.[MyEmployees Table].FirstName, dbo.[MyEmployees Table].LastName, dbo.[MyEmployees Table].Education, dbo.[MyEmployees Table].Occupation, dbo.Department.DepartmentName, dbo.[MyEmployees Table].YearlyIncome, dbo.[MyEmployees Table].Sales, dbo.[MyEmployees Table].HireDate FROM dbo.Department INNER JOIN dbo.[MyEmployees Table] ON dbo.Department.id = dbo.[MyEmployees Table].DeptID ORDER BY dbo.[MyEmployees Table].YearlyIncome DESC
Next, go to File Menu and select the Save option to save the designed View.
That will open the popup window called Choose Name to change the default name.
The following screenshot will show you the View that we created, and expand it to see the Column names.
NOTE: Some clients give access to Views only, and they will not allow you to access their main database.
SQL View using Create View Statement
This SQL Server view example shows how to create a View in SQL Server using the Create View Statement
-- SQL View Example USE [SQL Tutorial] GO CREATE VIEW EmployeesViewbyQuery AS SELECT [FirstName] + ' ' + [LastName] AS Name ,[Occupation] ,[Education] ,dept.DepartmentName AS Department ,[YearlyIncome] AS Income ,[Sales] FROM [MyEmployees Table] INNER JOIN Department AS dept ON Dept.[id] = [MyEmployees Table].DeptID GO
Let us see the Output
-- SQL View Example USE [SQL Tutorial] GO SELECT * FROM [dbo].[EmployeesViewbyQuery]
Rename Views in SQL Server
The SQL Server allows you to use the built-in stored procedure sp_rename, or the management studio to rename views in SQL Server.
Rename SQL View in SQL Management Studio
This example will show how to rename the View using SSMS.
To rename the SQL View using SSMS, Please navigate to the View that you want to modify (EmployeesViewbySSMS). And right-click on the view and select the Rename option
Once you click on the Rename option, SSMS allows us to rename as per our requirements.
Rename SQL View using SP_RENAME
We can use the sp_rename stored procedure to rename the view. The basic syntax behind this is:
SP_RENAME View_Old_Name, View_New_Name
Using the sp_rename stored procedure, you can rename the existing view. To do so, Click New query and return the following query.
-- Rename SQL View USE [SQL Tutorial] GO SP_RENAME EmployeesViewbyStudio, EmployeesViewbySQLSMS
Modify Views in SQL Server
The following examples help you understand the steps involved in modifying the SQL Views. First, let us see the limitations or restrictions to follow:
- Dependency Objects such as triggers or stored procedures will not affect by modifying a view.
- You can use the ALTER VIEW statement on indexed views because ALTER VIEW will unconditionally drop all the indexes on the views.
Modify Views using SSMS
To modify Views in Sql Server Management Studio, navigate to the view that you want to change (EmployeeViewbySQLSMS). Next, right-click on it and select the Design option
It opens a new design query window with existing tables, along with the relationship. Modify as per your requirement.
Let me select all the records from the EmployeeViewbySQLSMS to show the modified records.
-- Modified SQL View USE [SQL Tutorial] GO SELECT [FirstName] ,[LastName] ,[Education] ,[Occupation] ,[DepartmentName] ,[YearlyIncome] ,[Sales] ,[HireDate] FROM [dbo].[EmployeesViewbySQLSMS] GO
Alter View to Modify Views in SQL Server
The SQL Server ALTER VIEW statement is an ideal approach to modify the existing view. To do so, click the new query and write the subsequent query.
-- Modified SQL View USE [SQL Tutorial] GO ALTER VIEW [dbo].[EmployeesViewbySQLSMS] AS SELECT TOP 10 emp.FirstName, emp.LastName, emp.Education, emp.Occupation, Dept.DepartmentName, emp.YearlyIncome, emp.Sales FROM dbo.Department AS Dept INNER JOIN dbo.[MyEmployees Table] AS emp ON Dept.id = emp.DeptID ORDER BY emp.YearlyIncome DESC, emp.Sales DESC GO
Let us see the Output
-- Modified SQL View SELECT [FirstName] ,[LastName] ,[Education] ,[Occupation] ,[DepartmentName] ,[YearlyIncome] ,[Sales] FROM [SQL Tutorial].[dbo].[EmployeesViewbySQLSMS]
From the above screenshot, you can observe that View is returning top 10 records
Find definition Of a Views
How to use the sp_helptext stored procedure to get the definition of any view?.
-- SQL View Definition USE [SQL Tutorial] GO SP_HELPTEXT [EmployeesViewbyQuery]
Delete Views in SQL Server
The following examples show how to delete Views. Before we get into the examples, remember:
- Deleting, or Dropping a Table will not drop the dependent View. You have to explicitly use the DROP VIEW statement to delete Views in SQL Server.
- When you drop a view, all the view information (including the definition) will delete from the system catalog.
Delete Views in Management Studio
To delete a view in SQL Server using the Management Studio, right-click on the view name and click on the Delete option
For this demonstration, we want to delete EmployeesViewbySQLSMS
Selecting the delete option will open a Delete Object window. Click on the Show Dependencies button to check the dependencies, and then click OK to delete the view.
Delete Views or Drop View
Let me use the DROP VIEW statement to delete a SQL View.
-- Delete SQL View USE [SQL Tutorial] GO DROP VIEW [dbo].[EmployeesViewbyQuery] GO
TIP: It is good practice to check, whether the view exists in the database or not using IF OBJECT_ID (N’View_Name’, ‘V’) IS NOT NULL