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 數據