我想查询数据库 sys 表以获取表、列和主键信息的列表,如下查询:
select t.name as [TableName]
, (select max(column_id) from sys.columns c where c.object_id = t.object_id) as [ColumnCount]
, (select name from sys.columns c where c.object_id = t.object_id and c.column_id = 1) as [Column01]
, (select name from sys.columns c where c.object_id = t.object_id and c.column_id = 2) as [Column02]
, (select name from sys.columns c where c.object_id = t.object_id and c.column_id = 3) as [Column03]
, (select name from sys.columns c where c.object_id = t.object_id and c.column_id = 4) as [Column04]
, (select name from sys.columns c where c.object_id = t.object_id and c.column_id = 5) as [Column05]
, (select name from sys.columns c where c.object_id = t.object_id and c.column_id = 6) as [Column06]
, (select name from sys.columns c where c.object_id = t.object_id and c.column_id = 7) as [Column07]
, (select name from sys.columns c where c.object_id = t.object_id and c.column_id = 8) as [Column08]
, (select name from sys.columns c where c.object_id = t.object_id and c.column_id = 9) as [Column09]
, (select name from sys.columns c where c.object_id = t.object_id and c.column_id = 10) as [Column10]
, (select name from sys.columns c where c.object_id = t.object_id and c.column_id = 11) as [Column11]
, (select name from sys.columns c where c.object_id = t.object_id and c.column_id = 12) as [Column12]
, (select i.is_primary_key from sys.columns c inner join sys.index_columns ic on ic.column_id = c.column_id and ic.object_id = c.object_id inner join sys.indexes i on ic.index_id = i.index_id and ic.object_id = i.object_id and i.is_primary_key <> 0 where t.object_id = c.object_id and c.column_id = 1) as [Column01PrimaryKey]
, (select i.is_primary_key from sys.columns c inner join sys.index_columns ic on ic.column_id = c.column_id and ic.object_id = c.object_id inner join sys.indexes i on ic.index_id = i.index_id and ic.object_id = i.object_id and i.is_primary_key <> 0 where t.object_id = c.object_id and c.column_id = 2) as [Column02PrimaryKey]
, (select i.is_primary_key from sys.columns c inner join sys.index_columns ic on ic.column_id = c.column_id and ic.object_id = c.object_id inner join sys.indexes i on ic.index_id = i.index_id and ic.object_id = i.object_id and i.is_primary_key <> 0 where t.object_id = c.object_id and c.column_id = 3) as [Column03PrimaryKey]
, (select i.is_primary_key from sys.columns c inner join sys.index_columns ic on ic.column_id = c.column_id and ic.object_id = c.object_id inner join sys.indexes i on ic.index_id = i.index_id and ic.object_id = i.object_id and i.is_primary_key <> 0 where t.object_id = c.object_id and c.column_id = 4) as [Column04PrimaryKey]
, (select i.is_primary_key from sys.columns c inner join sys.index_columns ic on ic.column_id = c.column_id and ic.object_id = c.object_id inner join sys.indexes i on ic.index_id = i.index_id and ic.object_id = i.object_id and i.is_primary_key <> 0 where t.object_id = c.object_id and c.column_id = 5) as [Column05PrimaryKey]
, (select i.is_primary_key from sys.columns c inner join sys.index_columns ic on ic.column_id = c.column_id and ic.object_id = c.object_id inner join sys.indexes i on ic.index_id = i.index_id and ic.object_id = i.object_id and i.is_primary_key <> 0 where t.object_id = c.object_id and c.column_id = 6) as [Column06PrimaryKey]
, (select i.is_primary_key from sys.columns c inner join sys.index_columns ic on ic.column_id = c.column_id and ic.object_id = c.object_id inner join sys.indexes i on ic.index_id = i.index_id and ic.object_id = i.object_id and i.is_primary_key <> 0 where t.object_id = c.object_id and c.column_id = 7) as [Column07PrimaryKey]
, (select i.is_primary_key from sys.columns c inner join sys.index_columns ic on ic.column_id = c.column_id and ic.object_id = c.object_id inner join sys.indexes i on ic.index_id = i.index_id and ic.object_id = i.object_id and i.is_primary_key <> 0 where t.object_id = c.object_id and c.column_id = 8) as [Column08PrimaryKey]
, (select i.is_primary_key from sys.columns c inner join sys.index_columns ic on ic.column_id = c.column_id and ic.object_id = c.object_id inner join sys.indexes i on ic.index_id = i.index_id and ic.object_id = i.object_id and i.is_primary_key <> 0 where t.object_id = c.object_id and c.column_id = 9) as [Column09PrimaryKey]
, (select i.is_primary_key from sys.columns c inner join sys.index_columns ic on ic.column_id = c.column_id and ic.object_id = c.object_id inner join sys.indexes i on ic.index_id = i.index_id and ic.object_id = i.object_id and i.is_primary_key <> 0 where t.object_id = c.object_id and c.column_id = 10) as [Column10PrimaryKey]
, (select i.is_primary_key from sys.columns c inner join sys.index_columns ic on ic.column_id = c.column_id and ic.object_id = c.object_id inner join sys.indexes i on ic.index_id = i.index_id and ic.object_id = i.object_id and i.is_primary_key <> 0 where t.object_id = c.object_id and c.column_id = 11) as [Column11PrimaryKey]
, (select i.is_primary_key from sys.columns c inner join sys.index_columns ic on ic.column_id = c.column_id and ic.object_id = c.object_id inner join sys.indexes i on ic.index_id = i.index_id and ic.object_id = i.object_id and i.is_primary_key <> 0 where t.object_id = c.object_id and c.column_id = 12) as [Column12PrimaryKey]
from sys.tables t
;
有没有办法简化查询而不是继续手动添加更多列?