What is SCHEMABINDING in SQL Server Views? – Interview Question of the Week #245

Question: What does schema binding do for a view? It protects the view’s referenced objects from changes that would invalidate its definition. It doesn’t freeze all data or every possible change to a table.

A hammock depends on two firmly connected supporting trees

The original demonstration is pleasantly direct. Create a table and a view that selects its ID column:

CREATE TABLE dbo.Table1 (ID int, Col1 varchar(100));
GO
CREATE VIEW dbo.FirstView WITH SCHEMABINDING
AS SELECT ID FROM dbo.Table1;
GO
DROP TABLE dbo.Table1;

The DROP fails with error 3729 because FirstView references the table through schema binding. A change that affects the referenced ID column is also restricted. Ordinary inserts, updates and deletes are still permitted; unrelated schema changes can be permitted too.

Actual captured error 3729 states dbo.Table1 cannot be dropped because FirstView references it
The guarded scratch demonstration catches and displays the actual 3729 error. Schema binding prevents the attempted DROP; the lab transaction is rolled back after the test.

Now deliberately remove the binding:

ALTER VIEW dbo.FirstView AS SELECT ID FROM dbo.Table1;
GO
DROP TABLE dbo.Table1;
GO
SELECT * FROM dbo.FirstView;

The table can now be dropped. The final SELECT cannot resolve the view’s table, producing an invalid-object/binding error. Removing the protection did not repair the dependency.

Use two-part object names such as dbo.Table1 and explicitly named columns. The view’s referenced objects must be in the same database. Schema binding is a prerequisite for an indexed view, but adding this clause alone doesn’t materialize the view or create an index.

The snippets explain the sequence; they drop an object and are intended only for a disposable lab. The editable companion script adds a dedicated-database guard, refuses pre-existing names, catches the expected error and rolls back its demonstration objects. Run the sequence only in a disposable database; it intentionally attempts to drop a table.

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.


Discover more from SQL Authority with Pinal Dave

Subscribe to get the latest posts sent to your email.

SQL Scripts, SQL Server, SQL Table Operation, SQL View
Previous Post
How to Download the Latest SQL Server Docker Image? – Interview Question of the Week #244
Next Post
How to Know If Index is Forced on Query? – Interview Question of the Week #246

Related Posts

3 Comments. Leave new

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.