SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Find Foreign Keys


Find Foreign Keys

Author
Message
suba.sathyanathan 40131
suba.sathyanathan 40131
SSC-Enthusiastic
SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)

Group: General Forum Members
Points: 119 Visits: 164
Comments posted to this topic are about the item Find Foreign Keys
dbasnyat25
dbasnyat25
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 152
soory i am getting an error
suba.sathyanathan 40131
suba.sathyanathan 40131
SSC-Enthusiastic
SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)

Group: General Forum Members
Points: 119 Visits: 164
Not sure if there's any whitespace problem. Please use the below query.

;With CTE As
(Select Object_Name(constraint_object_id) Constraint_Name,
Object_Name(parent_object_id) Table_Name,
C.name Column_Name
From sys.foreign_key_columns FK
Inner Join sys.columns C
On FK.parent_object_id = C.object_id
And FK.parent_column_id = C.column_id)
Select C.Constraint_Name,
C.Table_Name,
C.Column_Name,
Object_Name(FK.referenced_object_id) Referenced_Table_Name,
SC.name Referenced_Column_Name
from CTE C
Inner Join sys.foreign_key_columns FK
On C.Constraint_Name = Object_Name(FK.constraint_object_id)
Inner Join sys.columns SC
On FK.referenced_object_id = SC.object_id
And FK.referenced_column_id = SC.column_id
suba.sathyanathan 40131
suba.sathyanathan 40131
SSC-Enthusiastic
SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)

Group: General Forum Members
Points: 119 Visits: 164
Not sure if there's any whitespace problem. Please use the below query.

;With CTE As
(Select Object_Name(constraint_object_id) Constraint_Name,
Object_Name(parent_object_id) Table_Name,
C.name Column_Name
From sys.foreign_key_columns FK
Inner Join sys.columns C
On FK.parent_object_id = C.object_id
And FK.parent_column_id = C.column_id)
Select C.Constraint_Name,
C.Table_Name,
C.Column_Name,
Object_Name(FK.referenced_object_id) Referenced_Table_Name,
SC.name Referenced_Column_Name
from CTE C
Inner Join sys.foreign_key_columns FK
On C.Constraint_Name = Object_Name(FK.constraint_object_id)
Inner Join sys.columns SC
On FK.referenced_object_id = SC.object_id
And FK.referenced_column_id = SC.column_id
houming1982
houming1982
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
Points: 14 Visits: 244
SELECT
FK_Table = FK.TABLE_NAME,
FK_Column = CU.COLUMN_NAME,
PK_Table = PK.TABLE_NAME,
PK_Column = PT.COLUMN_NAME,
Constraint_Name = C.CONSTRAINT_NAME
FROM
INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C
INNER JOIN
INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK
ON C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME
INNER JOIN
INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK
ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME
INNER JOIN
INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU
ON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME
INNER JOIN
(
SELECT
i1.TABLE_NAME, i2.COLUMN_NAME
FROM
INFORMATION_SCHEMA.TABLE_CONSTRAINTS i1
INNER JOIN
INFORMATION_SCHEMA.KEY_COLUMN_USAGE i2
ON i1.CONSTRAINT_NAME = i2.CONSTRAINT_NAME
WHERE i1.CONSTRAINT_TYPE = ‘PRIMARY KEY’
) PT
ON PT.TABLE_NAME = PK.TABLE_NAME
ORDER BY
FK_Column
Iwas Bornready
Iwas Bornready
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16334 Visits: 885
Thanks for the script.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search