How to write a sql query or script to get or
list out all the parent and child tables name of a given or specified table in
sql server
--Change the
table name
DECLARE @TableName
AS VARCHAR(100) = 'tblEmployee'
SELECT
OBJECT_NAME(fk.referenced_object_id) AS TableName,
'Parent' AS Relation
FROM sys.tables t INNER JOIN sys.foreign_keys fk
ON t.object_id = fk.parent_object_id
WHERE t.name = @TableName
UNION ALL
SELECT
OBJECT_NAME(fk.parent_object_id),
'Child'
FROM sys.tables t INNER JOIN sys.foreign_keys fk
ON t.object_id = fk.referenced_object_id
WHERE t.name = @TableName
1 comment:
Good one! Thanks for sharing. By the way What's the benifit of investing in funds over the individual stocks and bonds?
Sensex
Sensitive Index
BSE Sensex
Post a Comment