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