0

I'm trying to accomplish the following:

I have several tables, some of them containing primary keys from other tables.
I tried to prepare a SQLFiddle for clarification, you can find it here http://sqlfiddle.com/#!3/38ea2/1

+-----------+    +-----------+-----------+    +-----------+-----------+    +-----------+-----------+    
|   tblA    |    |          tblB         |    |          tblC         |    |          tblD         |
+-----------+    +-----------+-----------+    +-----------+-----------+    +-----------+-----------+
|   ID_A    |    |   ID_B    |   ID_A    |    |   ID_C    |   ID_B    |    |   ID_D    |   ID_C    |
+-----------+    +-----------+-----------+    +-----------+-----------+    +-----------+-----------+
|   1       |    |     2     |     1     |    |     3     |     2     |    |     4     |     3     |
+-----------+    +-----------+-----------+    +-----------+-----------+    +-----------+-----------+


My goal is to create a procedure which returns all the available relationships between the tables and visualize that at the end (using VB.NET).
The way I want to visualize this would be something like this:

ID_A(1)
    ID_B(2)
        ID_C(3)<-- current
            ID_D(4)

All of the items in this list are hyperlinked and lead to a site with their respective "journal", i.e. if you click on ID_B, then it would print:

ID_A(1)
    ID_B(2)<-- current
        ID_C(3)
            ID_D(4)

(I hope you guys know now where I'm coming from, since I'm struggling a bit with describing my problem.)

So I need a procedure/function with (at least) 2 parameters, @iID and @sFieldName, I think.
So calling GetRelationships(3, 'ID_C') should give back 2 (ID_B), 1 (ID_A) and 4 (ID_D) back, since that's how they are connected.

I think a good way to start would be collecting which tables (and their PKs) are connected and storing them in a temporary table, I am still playing around with this and will edit the opening post when I got something useful.

I would really appreciate some hints or ideas.

ps. I'm using SQL Server 2005

4

2 に答える 2

1
DECLARE @TableName NVARCHAR(1000)= 'TableName'
DECLARE @ColumnName NVARCHAR(1000)= 'ColumnName'


SELECT f.name AS ForeignKey,
 SCHEMA_NAME(f.SCHEMA_ID) SchemaName,
 OBJECT_NAME(f.parent_object_id) AS TableName,
 COL_NAME(fc.parent_object_id,fc.parent_column_id) AS ColumnName,
 SCHEMA_NAME(o.SCHEMA_ID) ReferenceSchemaName,
 OBJECT_NAME (f.referenced_object_id) AS ReferenceTableName,
 COL_NAME(fc.referenced_object_id,fc.referenced_column_id) AS ReferenceColumnName
 FROM sys.foreign_keys AS f
 INNER JOIN sys.foreign_key_columns AS fc ON f.OBJECT_ID = fc.constraint_object_id
 INNER JOIN sys.objects AS o ON o.OBJECT_ID = fc.referenced_object_id
WHERE OBJECT_NAME(f.parent_object_id) =  @TableName AND COL_NAME(fc.parent_object_id,fc.parent_column_id) = @ColumnName
 GO 
于 2013-10-08T16:32:40.807 に答える
1

再帰的なアプローチを検討する必要があります。基本的に、直接の依存関係を 1 つのレベルで解決するストアド プロシージャを作成します (つまり、テーブルへの外部キーとテーブルからの外部キー)。次に、結果を取得したら、結果ごとに再度呼び出します。完全な階層を構築するまで繰り返します。

外部関係は、sys.foreign_key_columnsおよびsys.objectsテーブルを介して SQL サーバーで照会できます。

この質問もご覧ください: SQL Server で特定のテーブルを参照するすべての外部キーを一覧表示するにはどうすればよいですか?

于 2013-10-08T15:39:48.363 に答える