SQL Server

SLQ Server Tutorial – How To Check Primary Key Exists or not in Table

This is a simple tip post that may seem obvious and taken for granted for those of us who have been working with SQL Server for a while now but may be a newbie will find this helpful.

Here, I will give you a simple tip to check that primary is exists or not in table.

TO check primary key existence in table we have to use the following query. In the following query I have used MyTestTable as an example table. You have to change your table name from this table name.

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

Happy coding!!

From http://manish4dotnet.blogspot.in/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s