由于名称规范化脚本错误,我有几个默认约束现在具有非法名称(显然sp_rename
在验证参数方面做得很差newname
)。
某些列具有格式中的实际默认名称
[[DF_SomeTable_SomeColumn]]
或者
[dbo.[DF_SomeOtherDefault]]
也sp_rename
不能Alter Table Drop Constraint
删除它们,因为它们被报告为语法错误。
我怎样才能摆脱它们,或重命名它们?
由于名称规范化脚本错误,我有几个默认约束现在具有非法名称(显然sp_rename
在验证参数方面做得很差newname
)。
某些列具有格式中的实际默认名称
[[DF_SomeTable_SomeColumn]]
或者
[dbo.[DF_SomeOtherDefault]]
也sp_rename
不能Alter Table Drop Constraint
删除它们,因为它们被报告为语法错误。
我怎样才能摆脱它们,或重命名它们?
最简单的方法可能是让 SQL 使用该quotename
函数为您提供正确转义的约束名称。您可以查询目录视图以根据表名为您生成删除语句。举个例子:
-- this weirdness will create a default constraint called [[my weird default]]
create table t(i int constraint [[[my weird default]]]]] default (1))
-- find all the constraints with square bracket literals on the tables I care about
select concat('alter table [', t.name, '] drop constraint ', quotename(c.name))
from sys.tables t
join sys.default_constraints c on c.parent_object_id = t.object_id
where t.name = 't'
and c.name like '%[[]%'
-- returns: alter table [t] drop constraint [[[my weird default]]]]]
显然,将适当的过滤添加到您的查询中。