您的位置:首页 > 其它

游标暂停和启用外键约束foreignkey

2011-12-15 13:57 281 查看
---------暂停外键约束
declare @Sql nvarchar(200);
declare mycursor cursor for (select  'ALTER TABLE ['  + b.name +  '] NOCHECK CONSTRAINT ' +  a.name +';'   
from  sysobjects  a ,sysobjects  b    
where  a.xtype ='f' and  a.parent_obj = b.id)
open mycursor
fetch next from mycursor into @Sql
while(@@fetch_status = 0)
begin
	exec(@Sql)
	fetch next from mycursor into @Sql;
end
close mycursor
deallocate mycursor

---------启动外键约束
declare @Sql nvarchar(200);
declare mycursor cursor for (select  'ALTER TABLE [' + b.name +  '] CHECK CONSTRAINT ' +  a.name +';'  
from  sysobjects  a ,sysobjects  b    
where  a.xtype ='f' and  a.parent_obj = b.id)
open mycursor
fetch next from mycursor into @Sql
while(@@fetch_status = 0)
begin
	exec(@Sql)
	fetch next from mycursor into @Sql;
end
close mycursor
deallocate mycursor
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐