How to check if a Constraint exists in Sql server?

前端 未结 13 2010
无人及你
无人及你 2020-11-29 15:03

I have this sql:

ALTER TABLE dbo.ChannelPlayerSkins
    DROP CONSTRAINT FK_ChannelPlayerSkins_Channels

but apparently, on some other databa

13条回答
  •  情歌与酒
    2020-11-29 15:45

    I use the following query to check for an existing constraint before I create it.

    IF (NOT EXISTS(SELECT 1 FROM sysconstraints WHERE OBJECT_NAME(constid) = 'UX_CONSTRAINT_NAME' AND OBJECT_NAME(id) = 'TABLE_NAME')) BEGIN
    ...
    END
    

    This queries for the constraint by name targeting a given table name. Hope this helps.

提交回复
热议问题