Created
November 2, 2012 16:45
-
-
Save duncansmart/4002600 to your computer and use it in GitHub Desktop.
TSQL - Get table columns, types, primary keys, etc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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