一键删除数据库所有的外键约束-FOREIGN_KEYS
程序员文章站
2022-07-02 12:24:44
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
上一篇: HTML表单模板