How to get list of all tables that has identity columns

前端 未结 5 789
小蘑菇
小蘑菇 2021-02-04 00:21

I would like to learn how to fetch list of all tables that has identity columns from a MS SQL database.

5条回答
  •  粉色の甜心
    2021-02-04 00:26

    SELECT 
      [schema] = s.name,
      [table] = t.name
    FROM sys.schemas AS s
    INNER JOIN sys.tables AS t
      ON s.[schema_id] = t.[schema_id]
    WHERE EXISTS 
    (
      SELECT 1 FROM sys.identity_columns
        WHERE [object_id] = t.[object_id]
    );
    

提交回复
热议问题