解决方法
您可以通过将sys.tables.object_id = sys.objects.parent_object_id加入这些对象类型来轻松地获取此信息.
DECLARE @sql NVARCHAR(MAX); SET @sql = N''; SELECT @sql = @sql + N' ALTER TABLE ' + QUOTENAME(s.name) + N'.' + QUOTENAME(t.name) + N' DROP CONSTRAINT ' + QUOTENAME(c.name) + ';' FROM sys.objects AS c INNER JOIN sys.tables AS t ON c.parent_object_id = t.[object_id] INNER JOIN sys.schemas AS s ON t.[schema_id] = s.[schema_id] WHERE c.[type] IN ('D','C','F','PK','UQ') ORDER BY c.[type]; PRINT @sql; --EXEC sys.sp_executesql @sql;
PRINT只是用于眼球 – 如果你有很多约束,它可能不会显示整个脚本,因为它限制在8K.在这些情况下,请参阅this tip以了解在运行之前验证脚本的其他方法.
一旦您对输出感到满意,请取消注释EXEC.