Login or Sign Up to become a member!

EXPERTS, INFORMATION, IDEAS & KNOWLEDGE

Social bookmarker Add this

Find Out If A Table Has An Identity Column

From Wiki

Jump to: navigation, search

Here are 2 ways to find out if a table has an identity column The first way is using the COLUMNPROPERTY function and the second way is using the OBJECTPROPERTY function

Use COLUMNPROPERTY and the syscolumns system table

  1. USE northwind
  2. GO
  3.  
  4. DECLARE @tableName VARCHAR(50)
  5. SELECT @tableName = 'orders'
  6.  
  7.  
  8. SELECT COUNT(name) AS HasIdentity
  9. FROM syscolumns
  10. WHERE OBJECT_NAME(id) = @tableName
  11. AND COLUMNPROPERTY(id, name, 'IsIdentity') = 1
  12. GO


Use OBJECTPROPERTY and the TableHasIdentity property name

  1. USE northwind
  2. GO
  3.  
  4. DECLARE @intObjectID INT
  5. SELECT @intObjectID =OBJECT_ID('orders')
  6.  
  7. SELECT COALESCE(OBJECTPROPERTY(@intObjectID, 'TableHasIdentity'),0) AS HasIdentity



Contributed by: --SQLDenis 20:46, 29 May 2008 (GMT)

Part of SQL Server Programming Hacks

Section Usefull Admin stuff For The Developer

293 Rating: 2.3/5 (3 votes cast)