2010-06-11 90 views
0

「許多」方面的所有表和字段,我有一個主表和其他幾個表,這些主表通過幾個一對多的關係與 有關。 如何在「多」一側找到與主表中主鍵 相關的所有表和字段?查找Sql Server 2005中與某個表

我知道我可以通過查詢INFORMATION_SCHEMA, 中的視圖來提取此信息,但我不知道我在哪裏可以找到此信息。

謝謝

回答

0

我找到了答案與SQL服務器組 我用下面的查詢一些幫助,它在關係的一邊和多邊返回了模式名稱,表格和字段名稱:

SELECT 
    SchemaParent.name AS ParentSchemaName, 
    TableParent.name AS ParentTableName, 
    ColumnParent.name AS ParentColumnName, 
    SchemaChild.name AS ChildSchemaName, 
    TableChild.name AS ChildTableName, 
    ColumnChild.name AS ChildColumnName 
FROM   
    sys.foreign_key_columns AS kc INNER JOIN 
     sys.objects AS TableChild ON kc.parent_object_id = TableChild.object_id INNER JOIN 
     sys.schemas AS SchemaChild ON TableChild.schema_id = SchemaChild.schema_id INNER JOIN 
     sys.objects AS TableParent ON kc.referenced_object_id = TableParent.object_id INNER JOIN 
     sys.schemas AS SchemaParent ON TableParent.schema_id = SchemaParent.schema_id INNER JOIN 
     sys.columns AS ColumnParent ON kc.referenced_object_id = ColumnParent.object_id AND kc.referenced_column_id = ColumnParent.column_id INNER JOIN 
     sys.columns AS ColumnChild ON kc.parent_object_id = ColumnChild.object_id AND kc.parent_column_id = ColumnChild.column_id 
ORDER BY ParentTableName, ChildTableName 
1

退房:

INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE INFORMATION_SCHEMA.CONSTRAINT_TABLE_USAGE

+0

感謝您的信息,我會檢查出來。 – bzamfir 2010-06-12 10:13:40