sql-server – 如何删除表上的所有默认约束约束

前端之家收集整理的这篇文章主要介绍了sql-server – 如何删除表上的所有默认约束约束前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何在sql 2005中删除属于特定表的所有默认约束?

解决方法

一个解决方from a search :(编辑为默认约束)
SET NOCOUNT ON

DECLARE  @constraintname SYSNAME,@objectid int,@sqlcmd         VARCHAR(1024)

DECLARE CONSTRAINTSCURSOR CURSOR  FOR
SELECT NAME,object_id
FROM   SYS.OBJECTS
WHERE  TYPE = 'D' AND @objectid = OBJECT_ID('Mytable')

OPEN CONSTRAINTSCURSOR

FETCH NEXT FROM CONSTRAINTSCURSOR
INTO @constraintname,@objectid

WHILE (@@FETCH_STATUS = 0)
BEGIN
    SELECT @sqlcmd = 'ALTER TABLE ' + OBJECT_NAME(@objectid) + ' DROP CONSTRAINT ' + @constraintname
    EXEC( @sqlcmd)
    FETCH NEXT FROM CONSTRAINTSCURSOR
    INTO @constraintname,@objectid
END

CLOSE CONSTRAINTSCURSOR
DEALLOCATE CONSTRAINTSCURSOR
原文链接:https://www.f2er.com/mssql/77259.html

猜你在找的MsSQL相关文章