Views in SQL Server

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, a view in SQL Server does not store a set of values in a database. When you use the SELECT Statement against a SQL 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 Management Studio to create or replace views. 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.

SQL Create View Statement

This example shows how to create View in SQL Server using the Create Statement

CREATE VIEW EmployeesViewbyQuery
SELECT  [FirstName] + ' ' + [LastName] AS Name
	,dept.DepartmentName AS Department
	,[YearlyIncome] AS Income
FROM [MyEmployees Table]
 	 Department AS dept ON
	    Dept.[id] = [MyEmployees Table].DeptID

Let us see the rows and columns result set

SELECT * FROM [dbo].[EmployeesViewbyQuery]
Create Views in SQL Server 14

Create View in SQL Server Management Studio

This example shows the steps involved to create View in SQL Server using the Management Studio. The restrictions to remember while creating are

  • It can be created only in the current database
  • It allows us to have a maximum of 1024 columns

To see the existing Sql Server Views, Please select the Database that has it. As you see, our database has none.

Right-click on the folder in Management Studio will open the context menu. To create it, Please select the shown option from it.

Create Views in SQL Server 2

Once you click on the option, the Query Designer opens in a separate window, and a Pop-up window to add the required tables.

Create Views in SQL Server 3

Here, you can add functions, tables, or views. From the below, you can observe that we selected the MyEmployees table and Department table.

Create Views in SQL Server 4

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.

Create Views in SQL Server 5

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.

Create Views in SQL Server 6

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.

Create Views in SQL Server 7

Once you selected the Descending Operation, it will show you the sorting representation

Create Views in SQL Server 8

The final select query that we designed

Create Views in SQL Server 9

Let us execute this Sql views query that we designed using the Management Studio and see the result

SELECT  TOP (100) PERCENT dbo.[MyEmployees Table].FirstName, 
        dbo.[MyEmployees Table].LastName, 
        dbo.[MyEmployees Table].Education, 
        dbo.[MyEmployees Table].Occupation, 
        dbo.[MyEmployees Table].YearlyIncome, 
        dbo.[MyEmployees Table].Sales, 
        dbo.[MyEmployees Table].HireDate
FROM dbo.Department
     dbo.[MyEmployees Table] ON  = dbo.[MyEmployees Table].DeptID
ORDER BY dbo.[MyEmployees Table].YearlyIncome DESC
Create Views in SQL Server 13

Next, go to File Menu and select the Save option to save the designed query.

Create Views in SQL Server 10

It will open the popup window called Choose Name to change the default name.

Create Views in SQL Server 11

Expand the folder, see the one that we created, and expand it to see the Column names.

Create Views in SQL Server 12

NOTE: Some clients give access to Views only, and they will not allow you to access their main database.

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 in SQL Management Studio

This example rename them using SSMS. And to rename it, Please navigate to the one that you want to modify (EmployeesViewbySSMS). And right-click on the name and select the Rename option

Create Views in SQL Server 15

Once you click on the Rename option, SSMS allows us to rename them.

Create Views in SQL Server 16

Rename SQL View using SP_RENAME

We can use the sp_rename stored procedure to rename the view. The sp_rename syntax is:

SP_RENAME View_Old_Name, View_New_Name

Using the sp_rename stored procedure, you can rename the existing. To do so, Click New query and return the following query.

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.
  • You can use the ALTER VIEW statement on indexed ones because it will unconditionally drop all the indexes.

Modify in SSMS

To modify in Management Studio, navigate to the one that you want to change (EmployeeViewbySQLSMS). Next, right-click on it and select the Design option

Create Views in SQL Server 17

It opens a new design query window with existing tables, along with the relationship.

Here, we are using the Filter (WHERE Clause) to restrict the Employees whose Occupation = Professional, and we are using two columns in ORDER BY Clause

Create Views in SQL Server 19

Let me select all the records from the EmployeeViewbySQLSMS to show the modified records.

SELECT [FirstName]
  FROM [dbo].[EmployeesViewbySQLSMS]
Create Views in SQL Server 20

Alter View to Modify

The SQL Server ALTER VIEW statement is an ideal approach to modify the existing ones. To do so, click the new query and write the subsequent query.

ALTER VIEW [dbo].[EmployeesViewbySQLSMS]
SELECT TOP 10 emp.FirstName, 
FROM  dbo.Department AS Dept
      dbo.[MyEmployees Table] AS emp ON = emp.DeptID
ORDER BY emp.YearlyIncome DESC, 
         emp.Sales DESC

Let us see the Output

SELECT [FirstName]
  FROM [EmployeesViewbySQLSMS]
Create Views in SQL Server 21

How to find definition Of the Views?

How to use the sp_helptext stored procedure to get the definition.

SP_HELPTEXT [EmployeesViewbyQuery]
Create Views in SQL Server 22

Delete Views in SQL Server

The following examples show how to delete it.

  • Deleting, or Dropping a Table will not drop the dependent View. You have to explicitly use the SQL Server DROP VIEW statement to delete them.
  • When you drop it, all the information (including the definition) will delete from the system catalog.

Management Studio

To delete using the Management Studio, right-click on the name and click on the Delete option. For this demonstration, we want to delete EmployeesViewbySQLSMS

Create Views in SQL Server 23

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.

Create Views in SQL Server 24

Delete or Drop Views

Let me use the DROP statement to delete.

DROP VIEW [dbo].[EmployeesViewbyQuery]

TIP: It is good practice to check whether it exists in the database or not using IF OBJECT_ID (N’View_Name’, ‘V’) IS NOT NULL

Comments are closed.