Link to home
Start Free TrialLog in
Avatar of Tacobell777
Tacobell777

asked on

Get referencing tables

whats the best way to get a list of tables that reference to a table?

Is it

- sp_fkeys | if so, how do I select the FKTABLE_NAME and FKCOLUMN_NAME into a variables so I can do something with it?
- query a view, if so, which one?
ASKER CERTIFIED SOLUTION
Avatar of ChrisFretwell
ChrisFretwell

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tacobell777
Tacobell777

ASKER

hmm no, the result sp_fkeys returns is more usefull to me, it contains the FKTABLE_NAME and FKCOLUMN_NAME
What did you want to do with it when you got it? We've often written routines to take the results of an sp (like spaceused) and insert them into a table (temp or permanant) then used them from there. You may want to do that, but it really depends on what you want to do with the results once you have them.
If a user wants to delete a record (web based) then I want to display a list of objects that depend on the record.
So the user clicks "yes delete"
- see what tables depend on it
- automatically check if those tables that reference to the table have a FK record related to its PK
- if it does, then display the objects that depend on this record
- if not, then delete the record

I am not using cascading deletes here, as it does not apply.
Ouch. I know what you want to do.Its not impossible but needs some thought. I'm sure someone will answer before I do, but I'll think on it.

I'm thinking either dynamic sql, or you write some views. Not sure yet.
Avatar of EugeneZ
you can try sp:

sp_MSdependencies

see sampes
http://www.winnetmag.com/SQLServer/Article/ArticleID/21882/SQLServer_21882.html

also check:
FIX: sp_MSdependencies May Run for a Long Time to Script Tables and Views
http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q308888