这个其实没有什么难度,只是一个语法定义而已,只是自己用的少,所以偶尔在用的时候就忘记了,写在这里,以方便查阅。
以下sql示例展示的功能:
1,
游标的格式与定义2,下面游标的功能是
禁用当前数据库中所有的约束declare @sql nvarchar(1000)
declare my_cur cursor local for
select 'ALTER TABLE [' + b.name + '] NoCHECK CONSTRAINT ' + a.name as 禁用约束 from sys.foreign_keys a ,sys.objects b
where a.parent_object_id = b.object_id and b.type='u'
open my_cur
fetch next from my_cur into @sql
while @@fetch_status=0
begin
exec (@sql)
fetch next from my_cur into @sql
end
close my_cur
deallocate my_cur
上面只是简单的定义了一个游标,游标的详解,请参考本站:
SQL基础教程及代码优化下载下面游标是
启用当前数据库中所有的约束declare @sql nvarchar(1000)
declare my_cur cursor local for
select 'ALTER TABLE [' + b.name + '] CHECK CONSTRAINT ' + a.name as 启用约束 from sys.foreign_keys a ,sys.objects b
where a.parent_object_id = b.object_id and b.type='u'
open my_cur
fetch next from my_cur into @sql
while @@fetch_status=0
begin
exec (@sql)
fetch next from my_cur into @sql
end
close my_cur
deallocate my_cur