Tags

, , , , , , , ,


SQL Puzzle | List of foreign keys with their tables in SQL Server

Write a T-SQL function List of foreign keys in tables in SQL Server.

Rules/Restrictions

  • The challenge is to do it with T-SQL statements only.
  • Add your solution(s) in the comments section or send you solution(s) to pawankkmr@gmail.com

SOLUTION – 1

--

SELECT OBJECT_NAME(fk.parent_object_id) tabName , COL_NAME(fkc.parent_object_id,fkc.parent_column_id) colName 
FROM sys.foreign_keys fk CROSS APPLY
(
	SELECT fkc.parent_object_id,fkc.parent_column_id FROM sys.foreign_key_columns fkc 
	WHERE fk.OBJECT_ID = fkc.constraint_object_id 	
)k
--

Add a comment if you have a solution in mind. I would love to learn it. We all need to learn.

Enjoy !!! Keep Learning

Pawan Khowal

Http://MSBISkills.com

Advertisements