Determine Whether a Given Column Name Is an Identity or Not

There are actually two ways in which you can find out whether a given column name is an identity or not in SQL Server:

  1. Method One:
    Select ColumnProperty(Object_id('Table_Name'), 'FieldName', 'IsIdentity')
  2. Method Two:
    Declare @colName varchar(100)Declare @RetColName varchar(100)Set @colName = 'FieldName' --Status column = 128 means its an identity columnSelect @RetColName=[name] from syscolumns where status=128 and id=(select idfrom sysobjects where name='Table_Name')If @colName = @RetColName   Print 'Its Identity'Else   Print 'Not an identity column'

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


Recent Articles: