find foreign key name | SQL Server Performance Forums

SQL Server Performance Forum – Threads Archive

find foreign key name

Hello, I need a query that returns me a foreign key from a specific column. This query returns all foreign keys from the table: SELECT SYSOBJECTS.NAME
FROM SYSOBJECTS, SYSCONSTRAINTS
WHERE SYSCONSTRAINTS.CONSTID = SYSOBJECTS.ID
AND SYSOBJECTS.XTYPE = ‘F’
AND SYSOBJECTS.PARENT_OBJ IN (SELECT OBJ2.ID
FROM SYSCOLUMNS, SYSOBJECTS OBJ2
WHERE OBJ2.ID = SYSCOLUMNS.ID
AND SYSCOLUMNS.NAME IN (‘COPR_LOCO_PROVISAO’)) Somebody can say me that clause is missing in this query? thanks, George.
maybe this gets you on track ..
exec sp_fkeys @pktable_name = @ParentTbName
, @pktable_owner = @ParentTbOwner
— [ , [ @pktable_qualifier = ] ‘pktable_qualifier’ ]
— { , [ @fktable_name = ] ‘fktable_name’ }
— [ , [ @fktable_owner = ] ‘fktable_owner’ ]
— [ , [ @fktable_qualifier = ] ‘fktable_qualifier’ ]

Hi,
You should avoid directly accessing System tables,
instead of that use system provided views. Execute following query and restrict it based on your table name and column name.
SELECT * FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
Your suggestions worked as I wanted!
thanks.
]]>