DECLARE @ESQL VARCHAR(1000);DECLARE FCursor CURSOR --定義游標FOR (SELECT 'ALTER TABLE '+O.name+' DROP CONSTRAINT '+F.name+';' AS CommandSQL from SYS.FOREI ...
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