What is Schemabinding?
In SQL Server, when we use the “WITH SCHEMABINDING” clause in the definition of an object (view or function), we bind the object to the schema of all the underlying tables and views. This means that the underlying tables and views cannot be modified in a way that would affect the definition of the schema-bound object. It also means that the underlying objects cannot be dropped. We can still modify those tables or views, as longs as we don’t affect the definition of the schema-bound object (view or function).
If we reference a view or function in a schema-bound object (view or function), then the underlying view or function must also be schema-bound. And we cannot change the collation of a database, if it contains schema-bound objects
For schema binding, SQL server requires a more specific and safer SELECT statement. There are a couple of restrictions, which we must remember:
We must use two-part names, when referencing tables, views or functions (SchemaName.ObjectName).
We cannot use SELECT *, so we must choose specific column names. Otherwise, we will get the following error:
Msg 1054, Level 15, State 6, Procedure vwName, Line x
Syntax ‘*’ is not allowed in schema-bound objects.
We cannot run sys.sp_refreshview on a schema-bound view (or sys.sp_refreshsqlmodule on a schema-bound function).
We will get an unhelpful error, something like:
Could not find object ‘vwName’ or you do not have permission.
Post a Comment
0 Comments