2010-04-28 17 views

Respuesta

7

Para SQL Server 2005 y hasta, usar algo como:

SELECT 
    name, OBJECT_NAME(parent_object_id) 'Table' 
FROM 
    sys.foreign_keys 
WHERE 
    referenced_object_id = OBJECT_ID('Your-referenced-table-name-here') 
2
-- To find all the foreign keys established to a table! 
-- Columns: FKTABLE_NAME, FKCOLUMN_NAME 
sp_fkeys @pktable_name='your table name here' 
0

Digamos que su nombre de la tabla es TABLEX. Si quieres conocer todas las relaciones de clave externa (columnas de TABLEX referenciadas en otras tablas y columnas de otras tablas referenciadas en TABLEX) se podía hacer esto:

select name 'ForeignKeyName', 
    OBJECT_NAME(referenced_object_id) 'RefrencedTable', 
    OBJECT_NAME(parent_object_id) 'ParentTable' 
from sys.foreign_keys 
where referenced_object_id = OBJECT_ID('TableX') or 
    parent_object_id = OBJECT_ID('TableX') 
Cuestiones relacionadas