Solved

Get referencing tables

Posted on 2004-09-15
7
1,138 Views
Last Modified: 2008-02-01
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?
0
Comment
Question by:Tacobell777
  • 3
  • 2
7 Comments
 
LVL 7

Accepted Solution

by:
ChrisFretwell earned 150 total points
Comment Utility
sp_depends tablename

this will return views and procedures that depend on your table.

sp_helpconstraint tablename

this will return foreign keys and any other contraints on your table

You can also use the information schema views. For example INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS might have what you are looking for.

You can query the system tables directly, but this is always something that is recommended against doing.  That said, since sp_depends and sp_helpconstraint are sprocs, you can probably read through them and figure out how to get what you want in exactly the format you want

0
 
LVL 17

Author Comment

by:Tacobell777
Comment Utility
hmm no, the result sp_fkeys returns is more usefull to me, it contains the FKTABLE_NAME and FKCOLUMN_NAME
0
 
LVL 7

Expert Comment

by:ChrisFretwell
Comment Utility
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.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 17

Author Comment

by:Tacobell777
Comment Utility
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.
0
 
LVL 7

Expert Comment

by:ChrisFretwell
Comment Utility
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.
0
 
LVL 42

Expert Comment

by:EugeneZ
Comment Utility
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
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Granting access to Microsoft SQL Server 17 24
ASP SQL Syntax Duplicate Key 7 63
c# code 19 55
SQL Connection (Error 18456) 14 27
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now