你可以试试这个 - 这将在系统目录视图中查找必要的信息并创建一个“drop” T-SQL 命令,你可以复制并执行该命令以摆脱系统命名的默认约束:
SELECT
DropCmd = 'ALTER TABLE ' + t.name + ' DROP CONSTRAINT ' + df.Name
FROM sys.default_constraints df
INNER JOIN sys.columns c ON df.parent_object_id = c.object_id AND df.parent_column_id = c.column_id
INNER JOIN sys.tables t ON c.object_id = t.object_id
WHERE
t.Name = 'YourTableNameHere'
AND c.name = 'YourColumnNameHere'
如果您需要将其放入您自己的脚本中 - 您可以将 分配DropCmd
给一个变量,然后使用动态 SQL 执行该 SQL 语句:
DECLARE @DropStatement NVARCHAR(200)
SELECT
@DropStatement = N'ALTER TABLE ' + t.name + N' DROP CONSTRAINT ' + df.Name
FROM sys.default_constraints df
INNER JOIN sys.columns c ON df.parent_object_id = c.object_id AND df.parent_column_id = c.column_id
INNER JOIN sys.tables t ON c.object_id = t.object_id
WHERE
t.Name = 'YourTableName'
AND c.name = 'YourColumnName'
EXEC sp_executesql @DropStatement