当前位置: 代码迷 >> Sql Server >> 想删除外键,但怎么判断该外键是否已经存在?多谢
  详细解决方案

想删除外键,但怎么判断该外键是否已经存在?多谢

热度:69   发布时间:2016-04-27 20:10:52.0
想删除外键,但如何判断该外键是否已经存在?谢谢
ALTER   TABLE   dbo.table1
DROP   CONSTRAINT   fk_foreignkey1

怎么再写个if   exists来判断这个外键只在存在的情况下才删除?
谢谢!!

------解决方案--------------------
if exists(select 1 from sysobjects where name= 'fk_foreignkey1 ' and xtype= 'F ')
ALTER TABLE dbo.table1 DROP CONSTRAINT fk_foreignkey1
------解决方案--------------------
if exists (
select 1 from sysobjects
where name= 'fk_foreignkey1 '
and type= 'f '
)
ALTER TABLE dbo.table1
DROP CONSTRAINT fk_foreignkey1
  相关解决方案