Created
July 31, 2024 13:16
-
-
Save saitorhan/0907f4d861eb5f69da78eba705d48f9c to your computer and use it in GitHub Desktop.
Get Tables Description On SQL Server
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
WITH ColumnInfo AS ( | |
SELECT | |
t.name AS TableName, | |
c.name AS ColumnName, | |
y.name AS DataType, | |
c.max_length AS MaxLength, | |
c.is_nullable AS IsNullable, | |
ISNULL(i.is_unique, 0) AS IsUnique, | |
ISNULL(ep.value, '') AS Description, | |
ISNULL(cc.definition, '') AS CheckConstraint, | |
fk.referenced_object_id AS ReferencedTableID, | |
fk.referenced_column_id AS ReferencedColumnID, | |
CASE WHEN pk.column_id IS NOT NULL THEN 'Evet' ELSE 'Hayır' END AS IsPrimaryKey, | |
CASE WHEN c.is_identity = 1 THEN 'Evet' ELSE 'Hayır' END AS IsIdentity | |
FROM | |
sys.columns c | |
INNER JOIN | |
sys.tables t ON c.object_id = t.object_id | |
INNER JOIN | |
sys.schemas s ON t.schema_id = s.schema_id | |
INNER JOIN | |
sys.types y ON c.user_type_id = y.user_type_id | |
LEFT JOIN | |
sys.index_columns ic ON ic.object_id = c.object_id AND ic.column_id = c.column_id | |
LEFT JOIN | |
sys.indexes i ON ic.object_id = i.object_id AND ic.index_id = i.index_id AND i.is_unique = 1 | |
LEFT JOIN | |
sys.extended_properties ep ON ep.major_id = c.object_id AND ep.minor_id = c.column_id AND ep.name = 'MS_Description' | |
LEFT JOIN | |
sys.check_constraints cc ON c.object_id = cc.parent_object_id AND c.column_id = cc.parent_column_id | |
LEFT JOIN | |
sys.foreign_key_columns fk ON c.object_id = fk.parent_object_id AND c.column_id = fk.parent_column_id | |
LEFT JOIN | |
(SELECT i.object_id, ic.column_id | |
FROM sys.indexes i | |
INNER JOIN sys.index_columns ic ON i.object_id = ic.object_id AND i.index_id = ic.index_id | |
WHERE i.is_primary_key = 1) pk ON c.object_id = pk.object_id AND c.column_id = pk.column_id | |
) | |
SELECT | |
TableName AS "Tablo Adı", | |
ColumnName AS "Kolon Adı", | |
CASE | |
WHEN DataType IN ('char', 'varchar', 'nchar', 'nvarchar') | |
THEN DataType + '(' + | |
CASE | |
WHEN MaxLength = -1 THEN 'MAX' | |
WHEN DataType IN ('nvarchar', 'nchar') THEN CAST(MaxLength / 2 AS NVARCHAR(10)) | |
ELSE CAST(MaxLength AS NVARCHAR(10)) | |
END + ')' | |
ELSE DataType | |
END AS "Veri Tipi", | |
CASE WHEN IsNullable = 1 THEN 'Evet' ELSE 'Hayır' END AS "Null?", | |
CASE WHEN IsUnique = 1 THEN 'Evet' ELSE 'Hayır' END AS "Tekil?", | |
IsPrimaryKey AS "PK?", | |
IsIdentity AS "Otomatik Artan?", | |
Description AS "Açıklama", | |
CheckConstraint AS "Check Kısıtlaması", | |
ISNULL((SELECT name FROM sys.tables WHERE object_id = ReferencedTableID), '') AS "Bağlı Olduğu Tablo", | |
ISNULL((SELECT name FROM sys.columns WHERE object_id = ReferencedTableID AND column_id = ReferencedColumnID), '') AS "Bağlı Olduğu Kolon" | |
FROM | |
ColumnInfo |
Author
saitorhan
commented
Jul 31, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment