简化SQL列查找查询

时间:2013-04-09 06:23:20

标签: sql sql-server

我想查询数据库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 
;

有没有办法简化查询而不是继续手动添加更多列?

1 个答案:

答案 0 :(得分:2)

尝试这样的事情 - 这能为您提供所需的信息吗?

SELECT
    ColumnName = c.Name,
    SchemaName = s.Name, 
    TableName = t.Name,
    i.Name, i.is_primary_key
FROM
    sys.columns c 
INNER JOIN 
    sys.tables t ON c.object_id = t.object_id
INNER JOIN 
    sys.schemas s ON t.schema_id = s.schema_id
INNER JOIN 
    sys.indexes i ON i.object_id = t.object_id

它应该为您提供列名,模式和表名,索引名以及该索引是否为主键