In this article, I am going to tell about the dropping of indexes in View in SQL Server.
Views are virtual tables in which data from one or more tables gives the result set as our SQL table does with rows and columns. A View doesn’t store the data permanently in the database and at the time of execution, only its result set gets determined.
When a View contains a large amount of rows and has complex logic in it then we can create an index on a View to improve the query performance. A View consists of a Unique Clustered Index and it is stored in the database as the clustered index does.
Now, let’s run a few scenarios to check when Clustered index which is created on a View gets dropped automatically.
First, I will create a table on which I will run those scenarios.
- CREATE TABLE[dbo]. [Customer]
- (
- [CustomerID][int] IDENTITY(1, 1) NOT NULL, [CustomerName][varchar](50) NOT NULL, [CEO][varchar](40) NULL, [Phone][varchar](20) NOT NULL PRIMARY KEY CLUSTERED(
- [CustomerID] ASC))
- GO
Now, I will create a View which will use this Customer table.
- -- Create view
- Create VIEW vw_customer
- WITH SCHEMABINDING
- AS
- SELECT CustomerID, CustomerName, CEO
- from dbo.Customer
- GO
Here in the definition of View, I have used WITH SCHEMABINDING which is necessary for creating an index on a View. This option simulates that we cannot delete any of the base table used in the View and in order to make any changes, first, we need to drop or alter the View.
Also, all the table references in a View should have two part naming convention (schemaname.tablename) as we have in vw_Customer view (dbo.Customer).



Viknaraj ManogararajahPosted Jul 14, 2018, 10:45 PM
Nice Article, Thank you for sharing.........
Hadshana KamalanathanPosted Jul 14, 2018, 7:06 PM
Thanks for sharing