MSSQL Views
MSSQL Views
MSSQL Views
This diagram illustrates the concept of a view that included columns from
more than one table. Here we have two tables named 'Table
A' and 'Table B,' and by using a SQL statement, a view is created
containing data from both tables. Views are a database object; that's why
it does not store physically. This feature makes views excellent for
abstracting or hiding complex queries.
Uses of views
The primary use of view in SQL Server is to implement the security
mechanism. It prevents users from seeing specific columns and rows from
tables. It only shows the data returned by the query that was declared
when the view was created. The rest of the information is completely
hidden from the end-user.
Types of views
The SQL Server categories the views into two types:
1. User-Defined Views
2. System-Defined Views
Syntax
Example
Now, we will create a view based on these tables. Thus, the below
example will create a view name "course" that creates a virtual table
made by taking data from both tables.
We can verify the view data using the SELECT statement as below:
Using sp_rename
Suppose we want to add one more column named 'city' in the above-
created view course_enrolled. To do this, we need to use the statements
as follows:
This option will display a new design query window with existing tables
and their relationship. Here we can make any changes to our views.
How to get views definition in SQL Server?
SQL Server provides the sp_helptext stored procedure that allows us to
get the information of any views. We can use the below syntax to see the
definition of a view:
1. SP_HELPTEXT view_name
Suppose you want to see the definition of a course_enrolled view. You can
do this as follows:
1. SP_HELPTEXT course_enrolled
SQL Server provides a DROP VIEW command to remove the view from a
database. If the view does not exist, we will get an error. We can use the
below syntax to remove a view:
Once we click on the Delete option, we will get a new window to confirm
the deletion process. Click on Yes to complete the deletion. We will make
sure that it will delete all permission of a view also.