domingo, 26 de enero de 2020

237. Query para obtener todas las relaciones existentes entre las tablas de sql server

Hola:

Les dejo un query para obtener todas las relaciones que hay entre tablas de sql server

SELECT
    fk.name 'FK Name',
    tp.name 'Tabla padre',
    cp.name, cp.column_id,
    tr.name 'Tabla de referencia',
    cr.name, cr.column_id
FROM 
    sys.foreign_keys fk
INNER JOIN 
    sys.tables tp ON fk.parent_object_id = tp.object_id
INNER JOIN 
    sys.tables tr ON fk.referenced_object_id = tr.object_id
INNER JOIN 
    sys.foreign_key_columns fkc ON fkc.constraint_object_id = fk.object_id
INNER JOIN 
    sys.columns cp ON fkc.parent_column_id = cp.column_id AND fkc.parent_object_id = cp.object_id
INNER JOIN 
    sys.columns cr ON fkc.referenced_column_id = cr.column_id AND fkc.referenced_object_id = cr.object_id
ORDER BY
    tp.name, cp.column_id


Felices Lineas

No hay comentarios.:

Publicar un comentario