patternsqlMinor
Index will be removed
Viewed 0 times
indexwillremoved
Problem
I'm trying to simply change the length of a column within a table through SQL Server Management Studio, but receive a 'Validation Warning':
I remember from experience that if I actually go through with this, the binding will not be readded. In the past I've had to manually write a script to re-add the view. This doesn't seem right - why would it just remove it without re-adding it at the end of the operation?
It makes me think I don't have something set up correctly, or am going about this the wrong way - am I? Or is it 'correct' procedure to have to manually re-add things in this way after table changes are made?
'MyTable' table
-Warning: The following schema-bound objects will be modified:
-View 'dbo.MyViewName': indexes and schema binding will be removed.I remember from experience that if I actually go through with this, the binding will not be readded. In the past I've had to manually write a script to re-add the view. This doesn't seem right - why would it just remove it without re-adding it at the end of the operation?
It makes me think I don't have something set up correctly, or am going about this the wrong way - am I? Or is it 'correct' procedure to have to manually re-add things in this way after table changes are made?
Solution
Creating a view with
To change the base table you need to remove the
To find out if the view is schema bound use below tsql:
To remove the
Then make changes to the table and then alter the view again now
from BOL :
SCHEMABINDING :
Binds the view to the schema of the underlying table or tables. When SCHEMABINDING is specified, the base table or tables cannot be modified in a way that would affect the view definition. The view definition itself must first be modified or dropped to remove dependencies on the table that is to be modified. When you use SCHEMABINDING, the select_statement must include the two-part names (schema.object) of tables, views, or user-defined functions that are referenced. All referenced objects must be in the same database.
Views or tables that participate in a view created with the SCHEMABINDING clause cannot be dropped unless that view is dropped or changed so that it no longer has schema binding. Otherwise, the Database Engine raises an error. Also, executing ALTER TABLE statements on tables that participate in views that have schema binding fail when these statements affect the view definition.
WITH SCHEMABINDING locks the underlying tables and prevents any changes that may change the table schema.To change the base table you need to remove the
SCHEMABINDING option from the objects or drop and recreate these objects once the table has been altered.To find out if the view is schema bound use below tsql:
select objectproperty(object_id('view_name'), 'IsSchemaBound') as SchemaBoundTo remove the
with schemabinding option, just alter the view defination :alter view schemaname.tablename
-- with schemabinding (remove it )
as
select some_column_name
from schema_name.table_nameThen make changes to the table and then alter the view again now
with schemabindingalter view schemaname.tablename
with schemabinding --(adding back )
as
select some_column_name
from schema_name.table_namefrom BOL :
SCHEMABINDING :
Binds the view to the schema of the underlying table or tables. When SCHEMABINDING is specified, the base table or tables cannot be modified in a way that would affect the view definition. The view definition itself must first be modified or dropped to remove dependencies on the table that is to be modified. When you use SCHEMABINDING, the select_statement must include the two-part names (schema.object) of tables, views, or user-defined functions that are referenced. All referenced objects must be in the same database.
Views or tables that participate in a view created with the SCHEMABINDING clause cannot be dropped unless that view is dropped or changed so that it no longer has schema binding. Otherwise, the Database Engine raises an error. Also, executing ALTER TABLE statements on tables that participate in views that have schema binding fail when these statements affect the view definition.
Code Snippets
select objectproperty(object_id('view_name'), 'IsSchemaBound') as SchemaBoundalter view schemaname.tablename
-- with schemabinding (remove it )
as
select some_column_name
from schema_name.table_namealter view schemaname.tablename
with schemabinding --(adding back )
as
select some_column_name
from schema_name.table_nameContext
StackExchange Database Administrators Q#48356, answer score: 4
Revisions (0)
No revisions yet.