DECLARE @SQLSTR VARCHAR(1000); DECLARE My_Cursor CURSOR --定义游标 FOR (select ‘ALTER TABLE ‘+o.name+‘ drop CONSTRAINT ‘+fk.name+‘;‘ AS Command from sys.foreign_keys fk JOIN sys.all_objects o ON fk.parent_object_id=o.object_id) --查出需要的集合放到游标中 OPEN My_Cursor; --打开游标 FETCH NEXT FROM My_Cursor INTO @SQLSTR; --读取第一行数据 WHILE @@FETCH_STATUS = 0 BEGIN exec(@SQLSTR); --UPDATE dbo.MemberAccount SET UserName = UserName + ‘A‘ WHERE CURRENT OF My_Cursor; --更新 --DELETE FROM dbo.MemberAccount WHERE CURRENT OF My_Cursor; --删除 FETCH NEXT FROM My_Cursor INTO @SQLSTR; --读取下一行数据 END CLOSE My_Cursor; --关闭游标 DEALLOCATE My_Cursor; --释放游标 GO
如果删除记录的时候可以 NOCheck CONSTRAINT
如果是删除表的话 NoCheck好像是不行的,必须 drop CONSTRAINT
时间: 2024-10-14 01:24:08