2013-04-09 3 views
0

Я хочу запросить таблицы сист базы данных, чтобы получить список таблицы, колонки и первичного ключа информации, как этот запрос:Упростить SQL поиск столбца запроса

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 
; 

Есть ли способ, чтобы упростить запрос вместо продолжаете добавлять дополнительные столбцы вручную?

ответ

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 

Это должно дать вам имя столбца, имя схемы и таблицы, имя индекса и будет ли или нет, что индекс является первичным ключом

+0

Это делает в некотором смысле, но мне было интересно, если там был способ получить SQL в том же формате, где он содержит имя таблицы, а затем все столбцы. Я думаю, что я буду использовать то, что вы мне дали, и как-то примените PIVOT. – JeffM

Смежные вопросы