Ramon Quintana

Ramon Quintana

  • NA
  • 404
  • 61.6k

Hi, i need to know the FK from one table

Nov 8 2018 1:37 PM
I wrote this code trying to know the relations amongs other tables from my BD but it didn´t bring nothing.
 
USE tuBaseDeDatos; 
GO  DECLARE @tabName AS Varchar(50) 
SET @tabName = 'dbo.TU_TABLA'--table´s name from fk´s are goint to get 
 SELECT OBJECT_NAME(f.constid) AS 'FKName', c.name AS 'ColName'
 FROM sysforeignkeys f INNER JOIN syscolumns c 
ON f.fkeyid = c.id AND f.fkey = c.colid
 WHERE fkeyid = OBJECT_ID(@tabName)
 

Answers (8)