select * from sysobjects where xtype = 'PK' and parent_obj in (select id from sysobjects where name=表名 and xtype = 'U')
if exists( select d.name from syscolumns a join sysobjects b on a.id=b.id join syscomments c on a.cdefault=c.id join sysobjects d on c.id=d.id where b.name='表名' and (a.name='字段名') )
1楼的是判断表里面是否存在约束 如果约束是'FK_HR_SYS_I_REFERENCE_HR_SYS_S'的话 也可以 select * from sysobjects where name='FK_HR_SYS_I_REFERENCE_HR_SYS_S' 假如能打出1表示存在
select * from sysobjects where xtype = 'PK' and parent_obj in (select id from sysobjects where name=表名 and xtype = 'U')
if exists(select 1 from sysobjects where id=object_id('[dbo].[UserInfo_ForeignKey_FromSiteID]') and xtype='F') ALTER TABLE [UserInfo] Drop CONSTRAINT UserInfo_ForeignKey_FromSiteID 好像这样可以
if exists(
select d.name
from syscolumns a
join sysobjects b on a.id=b.id
join syscomments c on a.cdefault=c.id
join sysobjects d on c.id=d.id
where b.name='表名'
and (a.name='字段名')
)
如果约束是'FK_HR_SYS_I_REFERENCE_HR_SYS_S'的话 也可以
select * from sysobjects where name='FK_HR_SYS_I_REFERENCE_HR_SYS_S'
假如能打出1表示存在
and parent_obj in (select id from sysobjects where name=表名 and xtype = 'U')
ALTER TABLE [UserInfo] Drop CONSTRAINT UserInfo_ForeignKey_FromSiteID
好像这样可以