DECLARE @ESQL VARCHAR(1000);
DECLARE FCursor CURSOR --定义游标
FOR (SELECT 'ALTER TABLE '+O.name+' DROP CONSTRAINT '+F.name+';' AS CommandSQL from SYS.FOREIGN_KEYS F
JOIN SYS.ALL_OBJECTS O ON F.PARENT_OBJECT_ID = O.OBJECT_ID WHERE O.TYPE = 'U' AND F.TYPE = 'F') --查出需要的集合放到游标中
OPEN FCursor; --打开游标
FETCH NEXT FROM FCursor INTO @ESQL; --读取第一行数据
WHILE @@FETCH_STATUS = 0
BEGIN
exec(@ESQL);
FETCH NEXT FROM FCursor INTO @ESQL; --读取下一行数据
END
CLOSE FCursor; --关闭游标
DEALLOCATE FCursor; --释放游标
GO