Note that there are some explanatory texts on larger screens.

plurals
  1. POSimplify SQL Column lookup Query
    primarykey
    data
    text
    <p>I want to query the database sys tables to get a list of table, column, and primary key information like this query:</p> <pre><code>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 &lt;&gt; 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 &lt;&gt; 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 &lt;&gt; 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 &lt;&gt; 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 &lt;&gt; 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 &lt;&gt; 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 &lt;&gt; 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 &lt;&gt; 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 &lt;&gt; 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 &lt;&gt; 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 &lt;&gt; 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 &lt;&gt; 0 where t.object_id = c.object_id and c.column_id = 12) as [Column12PrimaryKey] from sys.tables t ; </code></pre> <p>Is there a way to simplify the query instead of continuing to add more columns manually?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload