Skip to content

Instantly share code, notes, and snippets.

@tparton42
Last active January 10, 2016 02:57
Show Gist options
  • Save tparton42/43695714d2c4041cd2ce to your computer and use it in GitHub Desktop.
Save tparton42/43695714d2c4041cd2ce to your computer and use it in GitHub Desktop.
SQL Server method for determining if a specific table constraint exists.
DECLARE @constraintName varchar(200);
SET @constraintName = 'SomeConstraintName';
IF ( SELECT count(*) FROM sys.objects
WHERE type_desc LIKE '%CONSTRAINT'
AND OBJECT_NAME(object_id) = @constraintName) <> 0 BEGIN
PRINT 'FOUND Constraint: ' + @constraintName;
-- Perform some action...
END
ELSE
PRINT 'CANNOT FIND Constraint: ' + @constraintName;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment