Skip to content

Instantly share code, notes, and snippets.

@jrwarwick
Created June 3, 2022 21:44
Show Gist options
  • Save jrwarwick/a0a68bcd505f1964575297e524d0ec36 to your computer and use it in GitHub Desktop.
Save jrwarwick/a0a68bcd505f1964575297e524d0ec36 to your computer and use it in GitHub Desktop.
MSSQL Tables and Columns survey - inspection of all tables with column names via a temporary table
SET NOCOUNT ON
DECLARE @AllTables table (db_name nvarchar(128),schema_name nvarchar(128),table_name nvarchar(128), table_type_desc nvarchar(60), table_modify_date datetime, column_id int,column_name nvarchar(128),data_type nvarchar(128),max_length smallint,precision tinyint, is_nullable bit)
DECLARE @SQL nvarchar(4000)
SET @SQL='select ''?'' as db_name,
schema_name(tab.schema_id) as schema_name,
tab.name as table_name, tab.type_desc as table_type_desc, tab.modify_date as table_modify_date,
col.column_id, col.name as column_name,
t.name as data_type, col.max_length, col.precision, col.is_nullable
from [?].sys.tables as tab
inner join [?].sys.columns as col
on tab.object_id = col.object_id
left join [?].sys.types as t
on col.user_type_id = t.user_type_id
order by schema_name,
table_name,
column_id;
'
INSERT INTO @AllTables (db_name ,schema_name ,table_name, table_type_desc, table_modify_date,column_id ,column_name ,data_type ,max_length ,precision,is_nullable )
EXEC sp_msforeachdb @SQL
SET NOCOUNT OFF
SELECT * FROM @AllTables ORDER BY 1
go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment