by Tom Coope » Thu, 26 Feb 2009 13:06:57
If you are on SQL 2005 or later, you can use the sys.columns catalog view,
e.g.,
Declare @TableName varchar(50);
Set @TableName = 'YourTableName';
If Exists(Select *
From sys.columns
Where is_identity = 1
And Object_Name(object_id) = @TableName)
Begin
Print @TableName + ' has identity column';
End
Else
Begin
Print @TableName + ' does not have identity column';
End
If you are on SQL 2000 (or if you need to support both SQL 2000 and SQL 2005
or later), you can use the syscolumns table and the ColumnProperty function,
e.g.,
Declare @TableName varchar(50);
Set @TableName = 'YourTableName';
If Exists(Select *
From syscolumns
Where ColumnProperty(id, name, 'IsIdentity') = 1
And Object_Name(id) = @TableName)
Begin
Print @TableName + ' has identity column';
End
Else
Begin
Print @TableName + ' does not have identity column';
End
Tom