Skip to content

Instantly share code, notes, and snippets.

@duncansmart
Created November 2, 2012 16:45
Show Gist options
  • Save duncansmart/4002600 to your computer and use it in GitHub Desktop.
Save duncansmart/4002600 to your computer and use it in GitHub Desktop.
TSQL - Get table columns, types, primary keys, etc
select
SchemaName = schema_name(t.schema_id)
, TableName = t.name
, ColumnName = c.name
, IsPrimaryKey = (select count(*) from
sys.indexes as i join sys.index_columns as ic on i.OBJECT_ID = ic.OBJECT_ID and i.index_id = ic.index_id and ic.column_id = c.column_id
where i.is_primary_key = 1 and i.object_id = t.object_id)
, ColumnType = TYPE_NAME(c.system_type_id)
, MaxLength = c.max_length
from sys.tables t join sys.columns c on t.object_id = c.object_id
order by t.name , c.column_id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment