Last active
August 30, 2016 18:56
-
-
Save j-alberto/d2a2b6a63a1592ae9a845a45b5129545 to your computer and use it in GitHub Desktop.
SQLServer - Get data dictionary from all schemas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******** table descriptions & check constrints ********/ | |
select obj.name | |
,prop.value | |
,obj.type_desc | |
from sys.extended_properties as prop | |
inner join sys.objects obj on | |
obj.object_id = prop.major_id | |
where minor_id = 0 | |
and prop.name = 'MS_Description' | |
order by obj.type_desc desc, obj.name | |
/******** table columns ********/ | |
select schemas.name as 'schema' | |
,tab.name as 'table' | |
,col.name as 'column' | |
,types.name as 'type' | |
,col.max_length | |
,col.precision | |
,col.scale | |
,col.is_nullable | |
,col.is_identity | |
,prop.value | |
from sys.tables as tab | |
inner join sys.columns as col on | |
tab.object_id = col.object_id | |
inner join sys.systypes as types on | |
col.system_type_id = types.xusertype | |
inner join sys.schemas on | |
tab.schema_id = schemas.schema_id | |
left join sys.extended_properties as prop on | |
tab.object_id = prop.major_id | |
and col.column_id = prop.minor_id | |
where schemas.name in ('') | |
order by [table] | |
/******** indexes ********/ | |
SELECT S.NAME SCHEMA_NAME,T.NAME TABLE_NAME,I.NAME INDEX_NAME,C.NAME COLUMN_NAME | |
,i.type_desc | |
FROM SYS.TABLES T | |
INNER JOIN SYS.SCHEMAS S | |
ON T.SCHEMA_ID = S.SCHEMA_ID | |
INNER JOIN SYS.INDEXES I | |
ON I.OBJECT_ID = T.OBJECT_ID | |
INNER JOIN SYS.INDEX_COLUMNS IC | |
ON IC.OBJECT_ID = T.OBJECT_ID | |
INNER JOIN SYS.COLUMNS C | |
ON C.OBJECT_ID = T.OBJECT_ID | |
AND IC.INDEX_ID = I.INDEX_ID | |
AND IC.COLUMN_ID = C.COLUMN_ID | |
WHERE s.name in ('') | |
ORDER BY s.name, t.name | |
/******** constraints ********/ | |
SELECT OBJECT_NAME(OBJECT_ID) AS NameofConstraint, | |
SCHEMA_NAME(schema_id) AS SchemaName, | |
OBJECT_NAME(parent_object_id) AS TableName, | |
type_desc AS ConstraintType | |
FROM sys.objects | |
WHERE type_desc in ('FOREIGN_KEY_CONSTRAINT', 'PRIMARY_KEY_CONSTRAINT') | |
and SCHEMA_NAME(schema_id) in ('') | |
order by TableName, ConstraintType | |
GO |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment