Solved

SQL Server: need a list of referencing tables

Posted on 2013-11-05
4
272 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
Comment Utility
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
Comment Utility
Disregard the above, posted in the wrong question.
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 250 total points
Comment Utility
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
Comment Utility
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SQL server 2008 SP4 29 31
SQL Server 2016 Developers Edition Install 4 57
SQL JOIN 6 31
Row-Level Security 2 17
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
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.
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…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

743 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

15 Experts available now in Live!

Get 1:1 Help Now