How do you determine what SQL Tables have an identity column programmatically

后端 未结 13 2019
借酒劲吻你
借酒劲吻你 2020-11-30 23:44

I want to create a list of columns in SQL Server 2005 that have identity columns and their corresponding table in T-SQL.

Results would be something like:

Tab

13条回答
  •  醉梦人生
    2020-12-01 00:08

    By some reason sql server save some identity columns in different tables, the code that work for me, is the following:

    select      TABLE_NAME tabla,COLUMN_NAME columna
    from        INFORMATION_SCHEMA.COLUMNS
    where       COLUMNPROPERTY(object_id(TABLE_SCHEMA+'.'+TABLE_NAME), COLUMN_NAME, 'IsIdentity') = 1
    union all
    select      o.name tabla, c.name columna
    from        sys.objects o 
    inner join  sys.columns c on o.object_id = c.object_id
    where       c.is_identity = 1
    

提交回复
热议问题