日期:2014-05-17 浏览次数:20485 次
select 'alter table ['+c.name+'] add constraint '+b.name+' foreign key ('+d.name+') references '+ e.name +' ('+f.name+')'
from sysforeignkeys a
inner join sysobjects b on a.constid=b.id
inner join sysobjects c on a.fkeyid=c.id
inner join syscolumns d on a.fkeyid=d.id and a.fkey=d.colid
inner join sysobjects e on a.rkeyid=e.id
inner join syscolumns f on a.rkeyid=f.id and a.rkey=f.colid
或者不检查约束
select 'alter table ['+c.name+'] with nocheck add constraint '+b.name+' foreign key ('+d.name+') references '+ e.name +' ('+f.name+')'
from sysforeignkeys a
inner join sysobjects b on a.constid=b.id
inner join sysobjects c on a.fkeyid=c.id
inner join syscolumns d on a.fkeyid=d.id and a.fkey=d.colid
inner join sysobjects e on a.rkeyid=e.id
inner join syscolumns f on a.rkeyid=f.id and a.rkey=f.colid