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

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

Leave a comment