当前位置: 代码迷 >> SQL >> 提取MSSQL全部外键约束
  详细解决方案

提取MSSQL全部外键约束

热度:37   发布时间:2016-05-05 14:53:31.0
提取MSSQL所有外键约束

--提取所有外键信息并列出所在表

--select OBJECT_NAME(parent_object_id) 'table', * from sys.objects where type='F'

?

-- 删除所有外键的方法1

select 'alter table '+OBJECT_NAME(parent_object_id)+' drop constraint '+name from sys.objects where type='F'
?将查询的结果 复制出来执行一下就ok

?

--方法2

?

declare @sql varchar(max),
[email protected]_name varchar(128),
[email protected]_name varchar(128);
declare c cursor for
select OBJECT_NAME(parent_object_id), name from sys.objects where type='F'

open c
fetch next from c into @tab_name, @fk_name
while @@FETCH_STATUS=0
begin
?set @sql='';
?set @sql='alter table ' + @tab_name + ' drop constraint ' + @fk_name
?print @sql
?exec(@sql)
?
?fetch next from c into @tab_name, @fk_name
end
close c
deallocate c

  相关解决方案