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’
, as TableName
,      QUOTENAME( as SQLTableName
, as ColumnName
,      quotename( as SQLColumnName
, 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 != ‘sysname’
JOIN sys.tables                ta
on sc.object_id = ta.object_id
JOIN information_schema.tables ist
on = ist.table_name
WHERE ta.type = ‘U’

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: Logo

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s