select 'ALTER TABLE ' + cast(c.name as varchar(255)) + '
DROP CONSTRAINT '
+ cast(f.name as varchar(255))
from sysobjects f
inner join sysobjects c on f.parent_obj = c.id
inner join sysreferences r on f.id = r.constid
inner join sysobjects p on r.rkeyid = p.id
inner join syscolumns rc on r.rkeyid = rc.id and r.rkey1 = rc.colid
inner join syscolumns fc on r.fkeyid = fc.id and r.fkey1 = fc.colid
left join syscolumns rc2 on r.rkeyid = rc2.id and r.rkey2 = rc.colid
left join syscolumns
fc2 on r.fkeyid
= fc2.id and r.fkey2 = fc.colid
where f.type = 'F'
select 'ALTER TABLE ' + cast(c.name as varchar(255)) + ' ADD CONSTRAINT ' + cast(f.name as varchar(255)) + '
FOREIGN KEY (' +
cast(fc.name as varchar(255)) + ')
REFERENCES ' +
cast(p.name as varchar(255)) + ' (' + cast(rc.name as varchar(255)) + ')'
from sysobjects f
inner join sysobjects c on f.parent_obj = c.id
inner join sysreferences r on f.id = r.constid
inner join sysobjects p on r.rkeyid = p.id
inner join syscolumns rc on r.rkeyid = rc.id and r.rkey1 = rc.colid
inner join syscolumns fc on r.fkeyid = fc.id and r.fkey1 = fc.colid
left join syscolumns rc2 on r.rkeyid = rc2.id and r.rkey2 = rc.colid
left join syscolumns
fc2 on r.fkeyid
= fc2.id and r.fkey2 = fc.colid
where f.type = 'F'
No comments:
Post a Comment