9.7. About Schema Binding

Schema binding essentially takes the things that your view is dependent upon (tables or other views), and "binds" them to that view. The significance of this is that no one can make alterations to those objects (CREATE, ALTER) unless they drop the schema-bound view first.

Why would you want to do this? Well, there are a few reasons why this can come in handy:

  • It prevents your view from becoming "orphaned" by alterations in underlying objects. Imagine, for a moment, that someone performs a DROP or makes some other change (even deleting a column could cause your view grief) but doesn't pay attention to your view. Oops. If the view is schema bound, then this is prevented from happening.

  • To allow indexed views. If you want an index on your view, you must create it using the SCHEMABINDING option. (We'll look at indexed views just a few paragraphs from now.)

  • If you are going to create a schema-bound user-defined function (and there are instances where your UDF must be schema bound) that references your view, then your view must also be schema bound.

Keep these in mind as you are building your views.

Get Professional SQL Server™ 2005 Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.