Find out which tables have a Primary Key

September 4th, 2009 § 0

If you want to do transactional replication, all the tables in the publication need to have a primary key. Here’s a quick way to determine which tables have a primary key.

This is the query for SQL 2005 and 2008:

SELECT so.name AS TableName, OBJECTPROPERTY(so.object_id, 'TableHasPrimaryKey') AS HasPrimaryKey
FROM sys.objects so
WHERE type = 'U'

And here is the query for SQL 2000:

SELECT so.name AS TableName, OBJECTPROPERTY(so.id, 'TableHasPrimaryKey') AS HasPrimaryKey
FROM sysobjects so
WHERE type = 'U'

Tagged: , ,

§ Leave a Reply

What's this?

You are currently reading Find out which tables have a Primary Key at SQLPS.com.

meta