Solved

SQL Server: need a list of referencing tables

Posted on 2013-11-05
4
273 Views
Last Modified: 2013-11-19
Hi, I need a list of tables which foreign keys reference primary key of the current table.
Thanks.
0
Comment
Question by:quasar_ee
  • 2
4 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39625210
Copy-paste the below T-SQL into your SSMS, execute it to verify it works, then delete the WHERE clause
CREATE TABLE boo (foo varchar(100) DEFAULT 'banana')

SELECT t.name, c.name, dc.definition
FROM sys.tables t
	JOIN sys.columns c ON t.object_id = c.object_id
	JOIN sys.default_constraints dc ON c.default_object_id = dc.object_id
WHERE c.name = 'foo'

Open in new window

0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39625255
Disregard the above, posted in the wrong question.
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 250 total points
ID: 39625815
You can get that by querying sys.objects and sys.foreign_key_columns.
0
 
LVL 26

Assisted Solution

by:Zberteoc
Zberteoc earned 250 total points
ID: 39633574
One quick way is to execute the following in the database where your table is:

USE YourDatabase
GO
EXEC sp_help table_name

Open in new window


And you will get info in multiple result sets about the table. The last 2 results sets will be about FKs from your table to others and FKs from other tables referencing your table.

If you only want the constraints you can run:

USE YourDatabase
GO
EXEC sys.sp_helpconstraint table_name

Open in new window

0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

911 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

18 Experts available now in Live!

Get 1:1 Help Now