A simple definition: Schema binding is a mode of ensuring that the objects referenced within a function or view, do not changes their definition in any way tat would crack the binded objects.
With SCHEMABINDING the obstacle is that you need to use
schema qualified names for everything. You will get lots of error messages like
this
Lets stimulate the same error with the below sample.
Here we are creating table without any qualified schema
create table test1
(col1 int,
col2 varchar(10),
col3 varchar(10))
go
Since our table is ready lets try to create a view and
refer the above table in it.
create view vw_test1
with schemabinding
as
select col1,col2 from test1
Msg 4512, Level
16, State 3, Procedure vw_test1, Line 10
Cannot schema
bind view 'vw_test1' because name 'test1' is invalid for schema binding. Names
must be in two-part format and an object cannot reference itself.
Here we can see our code fall in error, since we have
not specified any schema for our view. Lets add a schema and bind our above
view to the newly created schema.
create view dbo.vw_test1
with schemabinding
as
select col1,col2 from test1
Msg 4512, Level
16, State 3, Procedure vw_test1, Line 10
Cannot schema
bind view 'dbo.vw_test1' because name 'test1' is invalid for schema binding.
Names must be in two-part format and an object cannot reference itself.
So you need everything i.e every object to be schema
bounded.
Lets drop the table and recreate it with full qualified
name
drop table test1
Command(s) completed
successfully.
create schema test
Command(s) completed
successfully.
create table test.test1
(col1 int,
col2 varchar(10),
col3 varchar(10))
go
Command(s) completed
successfully.
Lets now create view with fully qualified name
create view dbo.vw_test1
with schemabinding
as
select col1,col2 from test.test1
Command(s) completed
successfully.
col1 col2 col3
----------- ----------
----------
(0 row(s) affected)
Conclusion: You need to create every object under full
qualified name i.e. defined with schema while using SCHEMABINDING option.
Leave everything and do your coding;
that’s the way to happiness. Just for SQL lovers :)
Post Reference: Vikram Aristocratic Elfin Share
No comments:
Post a Comment