Sunday, January 24, 2010

Check if a Primary key exists in a Table or not

This code will check if a Primary key is present in a table
In the below query, I'm checking in a Userdetails table. You can give your table name.

IF OBJECTPROPERTY( OBJECT_ID( '[dbo].[UserDetails]' ), 'TableHasPrimaryKey' ) = 1
BEGIN
PRINT 'Table UserDetails has primary key.'
END
ELSE
BEGIN
PRINT 'Table UserDetails does not have primary key.'
END

No comments:

Post a Comment