2012-07-04 187 views
1

我試圖得到一些查詢將返回以下內容:名單表名和列

TableName | ColumnName | IsColumnInRelation | RelatedTable | RelationKind | InsertUpdateSepecification | IsNull | Describtion 
------------------------------------------------------------------------------------------------------------------------------------- 
    Company | Personel |  Yes   | Employee |  One  |   Cascade   | True | 'company employees' 
    Company |  ID  |  No   | -------- |  ---  |   -------   | False | 'company pk' 
    Company |  Name |  No   | -------- |  ---  |   -------   | False | 'name of company' 
    Company | Country |  Yes   | Contries |  Many |   No Action  | False | 'companies placement' 
    Company |  .  |   .   |  .  |  .  |    .    | . | '' 
    Company |  .  |   .   |  .  |  .  |    .    | . | '' 
    Company |  .  |   .   |  .  |  .  |    .    | . | '' 
    Company |  .  |   .   |  .  |  .  |    .    | . | '' 

任何人都可以請幫助? 謝謝。

回答

1

這應該給你你在找什麼:

SELECT t.name AS [TableName] 
    ,c.name AS [ColumnName] 
    ,CASE WHEN COALESCE(tr.name, tr2.name) IS NULL THEN 'No' 
     ELSE 'Yes' END AS IsColumnInRelation 
    ,COALESCE(tr.name, tr2.name) AS [RelatedTable] 
    ,COALESCE(cr.name, cr2.name) AS [RelatedColumn] 
    ,CASE WHEN COALESCE(tr.name, tr2.name) IS NOT NULL THEN 
     CASE WHEN i.object_id IS NOT NULL THEN 'One' 
     ELSE 'Many' END 
    END AS RelationKind 
    ,COALESCE(f.update_referential_action_desc, f2.update_referential_action_desc) AS [InsertUpdateAction] 
    ,COALESCE(f.delete_referential_action_desc, f2.delete_referential_action_desc) AS [DeleteAction] 
    ,c.is_nullable AS [IsNull] 
    ,p.value AS Description 
FROM sys.columns c 
    INNER JOIN sys.tables t 
     ON t.object_id= c.object_id 

    -- Used for the column description. 
    LEFT JOIN sys.extended_properties p 
     ON c.object_id = p.major_id 
     AND c.column_id = p.minor_id 
     AND p.name = 'MS_Description' 

    -- Used to identify if this is a primary key. 
    -- If it is, then this is on the "One" side of the relationship. 
    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 i.is_primary_key = 1 

    -- Used to get the tables and columns that relate to this column. 
    LEFT JOIN sys.foreign_key_columns AS fc 
     ON c.object_id = fc.parent_object_id 
     AND c.column_id = fc.parent_column_id 
    LEFT JOIN sys.columns cr 
     ON cr.object_id = fc.referenced_object_id 
     AND cr.column_id = fc.referenced_column_id 
    LEFT JOIN sys.tables tr 
     ON cr.object_id = tr.object_id 

    -- Used to get the update/delete action for the [fc] relationship. 
    LEFT JOIN sys.foreign_keys f 
     ON f.object_id = fc.constraint_object_id 

    -- Used to get the tables and columns that this column relates to. 
    LEFT JOIN sys.foreign_key_columns AS fc2 
     ON c.object_id = fc2.referenced_object_id 
     AND c.column_id = fc2.referenced_column_id 
    LEFT JOIN sys.columns cr2 
     ON cr2.object_id = fc2.parent_object_id 
     AND cr2.column_id = fc2.parent_column_id 
    LEFT JOIN sys.tables tr2 
     ON cr2.object_id = tr2.object_id 

    -- Used to get the update/delete action for the [fc2] relationship. 
    LEFT JOIN sys.foreign_keys f2 
     ON f2.object_id = fc2.constraint_object_id 
ORDER BY t.name, c.name, COALESCE(tr.name, tr2.name), COALESCE(cr.name, cr2.name) 
+0

這將返回我一些重複列 – Mohsen

0

這裏有些東西可以幫助你。 它顯示所有你的數據庫的關係:

SELECT 
K_Table = FK.TABLE_NAME, 
FK_Column = CU.COLUMN_NAME, 
PK_Table = PK.TABLE_NAME, 
PK_Column = PT.COLUMN_NAME, 
Constraint_Name = C.CONSTRAINT_NAME 
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C 
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK ON C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME 
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME 
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU ON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME 
INNER JOIN (
SELECT i1.TABLE_NAME, i2.COLUMN_NAME 
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS i1 
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE i2 ON i1.CONSTRAINT_NAME = i2.CONSTRAINT_NAME 
WHERE i1.CONSTRAINT_TYPE = 'PRIMARY KEY' 
) PT ON PT.TABLE_NAME = PK.TABLE_NAME