--如果你不知道约束名,可以这样处理 declare tb cursor local for select sql='alter table ['+c.name+'] drop constraint ['+b.name+']' from sysforeignkeys a join sysobjects b on b.id=a.constid join sysobjects c on c.id=a.fkeyid join syscolumns d on c.id=d.id and a.fkey=d.colid join sysobjects e on e.id=a.rkeyid join syscolumns f on e.id=f.id and a.rkey=f.colid where b.name='B' and d.name='A' and c.name='PA' and e.name='CB' group by a.namedeclare @s varchar(8000) open tb fetch next from tb into @s while @@fetch_status=0 begin exec(@s) fetch next from tb into @s end close tb deallocate tb
constraing 寫錯了吧
alter table B drop CONSTRAINT 约束名
declare tb cursor local for
select sql='alter table ['+c.name+'] drop constraint ['+b.name+']'
from sysforeignkeys a
join sysobjects b on b.id=a.constid
join sysobjects c on c.id=a.fkeyid
join syscolumns d on c.id=d.id and a.fkey=d.colid
join sysobjects e on e.id=a.rkeyid
join syscolumns f on e.id=f.id and a.rkey=f.colid
where b.name='B' and d.name='A'
and c.name='PA' and e.name='CB'
group by a.namedeclare @s varchar(8000)
open tb
fetch next from tb into @s
while @@fetch_status=0
begin
exec(@s)
fetch next from tb into @s
end
close tb
deallocate tb