Skip to content

Instantly share code, notes, and snippets.

@jsm85
Created April 25, 2016 14:06
Show Gist options
  • Save jsm85/a3f488f46a6c5394525fcd3840089705 to your computer and use it in GitHub Desktop.
Save jsm85/a3f488f46a6c5394525fcd3840089705 to your computer and use it in GitHub Desktop.
A query to get all table and column definitions for a database
select distinct
ROW_NUMBER() over (Order by s.name, t.name) as Id,
s.name as SchemaName,
t.name as TableName,
c.name as ColumnName,
typ.name as DataType,
case c.precision
when 0 then 'Max'
else cast(c.precision as nvarchar(75))
end as DataTypeValue,
c.is_identity as IsPrimaryKey
from
sys.tables t
inner join
sys.schemas s on t.schema_id = s.schema_id
inner join
sys.columns c on c.object_id = t.object_id
inner join
sys.types typ on c.user_type_id = typ.user_type_id
order by
s.name, t.name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment