List all tables and columns in database

In a recent data warehouse project we had to do source to target mappings (STTM) for our ETL. I want to return a list of all tables with their supporting information such as schema’s, columns and data types. Something similar to this:

TablelistNotice the SQL* columns. This is added to ensure we can use the values in SQL Select statements in our SSIS datasource components.

Here is the TSQL against the system objects that will return a list of all tables and column information you require for the STTM’s.

SELECT ist.table_catalog as ‘Database’
,      schema_name(ta.schema_id) as ‘Schema’
,      ta.name as TableName
,      QUOTENAME(ta.name) as SQLTableName
,      sc.name as ColumnName
,      quotename(sc.name) as SQLColumnName
,      t.name as DataType
,      sc.Max_Length
,      sc.Precision
,      sc.Scale
FROM sys.columns               sc
JOIN sys.types                 t
on t.system_type_id = sc.system_type_id
and t.name != ‘sysname’
JOIN sys.tables                ta
on sc.object_id = ta.object_id
JOIN information_schema.tables ist
on ta.name = ist.table_name
WHERE ta.type = ‘U’
ORDER BY ta.name
,        sc.name

Advertisements
This entry was posted in SQL Server 2012 and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s