Saturday, April 13, 2013

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.

You can find some other articles and tips related to C#, ASP.Net and SQL Server in this blog.

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.'

I hope you liked it. I would like to have any feedback from you. Your valuable feedback, question, or comments about this article are always welcome.

No comments:

Post a Comment

If You Enjoyed This Post Please Take 5 Seconds To Share It.

^ Scroll to Top hgpromo