Home > Archive > Microsoft SQL Server forum > July 2005 > Related tables









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Related tables
newtophp2000@yahoo.com

2005-07-01, 11:23 am

Is it possible to retrieve all tables that a given one is related to
via foreign keys?

Razvan Socol

2005-07-02, 7:23 am

Try this:

SELECT DISTINCT o1.name FROM sysreferences r
INNER JOIN sysobjects o1 ON o1.id=r.fkeyid
INNER JOIN sysobjects o2 ON o2.id=r.rkeyid
WHERE o2.name='YourTable'
/*
UNION
SELECT DISTINCT o2.name FROM sysreferences r
INNER JOIN sysobjects o1 ON o1.id=r.fkeyid
INNER JOIN sysobjects o2 ON o2.id=r.rkeyid
WHERE o1.name='YourTable'
*/

Razvan

newtophp2000@yahoo.com

2005-07-06, 3:23 am

Razvan Socol wrote:
> Try this:
>
> SELECT DISTINCT o1.name FROM sysreferences r
> INNER JOIN sysobjects o1 ON o1.id=r.fkeyid
> INNER JOIN sysobjects o2 ON o2.id=r.rkeyid
> WHERE o2.name='YourTable'
> /*
> UNION
> SELECT DISTINCT o2.name FROM sysreferences r
> INNER JOIN sysobjects o1 ON o1.id=r.fkeyid
> INNER JOIN sysobjects o2 ON o2.id=r.rkeyid
> WHERE o1.name='YourTable'
> */



Thanks, this gets me closer to the goal. By the way, if I try it as it
is, it returns no rows at first. When I remove the WHERE clause, then
it works!

I will need to look more into the system tables.

Sponsored Links





Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive | Programming forum archive

Copyright 2009 droptable.com