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

您应该考虑递归方法。基本上编写一个存储过程来解决一个级别的直接依赖关系(即外键进入你的表,外键来自你的表)。然后,一旦你得到结果,再次调用每个结果。重复直到建立完整的层次结构。

对外关系可以通过sys.foreign_key_columnssys.objects表在 SQL Server 中查询。

也看看这个问题:如何列出引用 SQL Server 中给定表的所有外键?

于 2013-10-08T15:39:48.363 回答
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 回答