SQL Server – Check Constraint Status

Below is a script that you can use to check the constraint status of all your tables in the database.

 

SELECT​​ (CASE​​ 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ WHEN​​ OBJECTPROPERTY(CONSTID,​​ 'CNSTISDISABLED')​​ =​​ 0​​ THEN​​ 'ENABLED'

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ ELSE​​ 'DISABLED'

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ END)​​ AS​​ STATUS,

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ OBJECT_NAME(CONSTID)​​ AS​​ CONSTRAINT_NAME,

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ OBJECT_NAME(FKEYID)​​ AS​​ TABLE_NAME,

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ COL_NAME(FKEYID,​​ FKEY)​​ AS​​ COLUMN_NAME,

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ OBJECT_NAME(RKEYID)​​ AS​​ REFERENCED_TABLE_NAME,

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ COL_NAME(RKEYID,​​ RKEY)​​ AS​​ REFERENCED_COLUMN_NAME

 ​​ ​​​​ FROM​​ SYSFOREIGNKEYS

ORDER​​ BY​​ TABLE_NAME,​​ CONSTRAINT_NAME,REFERENCED_TABLE_NAME,​​ KEYNO

 

 

 

Output:

 

Knowledge worth sharing...Share on linkedin
Linkedin
Share on facebook
Facebook
Share on google
Google
Share on twitter
Twitter
Bookmark the permalink.

Leave a Reply

Leave a Reply

Your email address will not be published. Required fields are marked *