首頁  >  文章  >  資料庫  >  去除外键的存储过程

去除外键的存储过程

WBOY
WBOY原創
2016-06-07 14:54:36981瀏覽

去除外键的存储过程 外键 存储过程 create proce prdropforeignkeys@chvreferencedtable varchar(30)as declare @chvtablewithforeignkey varchar(30), @chvforeignkey varchar(30), @chvsql varchar(255)declare cufks cursor for select tb.name,fk.name f

去除外键的存储过程 外键 存储过程
create proce prdropforeignkeys
@chvreferencedtable varchar(30)
as
 declare @chvtablewithforeignkey varchar(30),
            @chvforeignkey varchar(30),
            @chvsql varchar(255)
declare cufks cursor for 
 select  tb.name,fk.name from 
((sysobject tb inner join sysreferences r on tb.id=r.fkeyid)
  inner join sysobjects fk on r.constid=fk.id)
  inner join sysobjects refd on refd_tb.id=r.rkeyid)
 where refd_tb.name=@chvreferencedtable
 open cufks
 fetch next from cufks into @chvtablewithforeignkey,@chvforeignkey 

 while (@@fetch_status<>-1)
 begin
  select @chvsql='alter table'+@chvtablewithforeignkey+
    'drop constraint '+@chvforeignkey
  exec(@chvsql)
 fetch next from cufks into @chvtablewithforeignkey,@chvforeignkey
end
 deallocate cufks
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn