To check a primary key exists or not in table

Use the following Query to check whether the Table does have "Primary Key" or not.

Lets assume its Employee Table here. the Query is as Follows :

Example :
    IF OBJECTPROPERTY( OBJECT_ID( '[dbo].[Employee]' ), 'TableHasPrimaryKey' ) = 1
        PRINT '[dbo].[Employee] table has a primary key.'
    ELSE
        PRINT '[dbo].[Employee] table has no primary key.'

No comments:

Post a Comment