gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 12:35:59 25 4
gpt4 key购买 nike

如何删除属于 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

关于sql-server - 如何删除表上的所有默认约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/767248/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com