Advertisement
EmilianoRoldanR

Saber cuantas referencias tiene un atributo de una tabla en el resto de la base de datos

Nov 28th, 2023
1,375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 0.83 KB | None | 0 0
  1. -- Especifica el nombre de la tabla y el atributo que estás buscando
  2. DECLARE @NombreTabla NVARCHAR(128) = '';
  3. DECLARE @NombreAtributo NVARCHAR(128) = '';
  4.  
  5. -- Consulta para obtener las claves foráneas que hacen referencia al atributo en la tabla
  6. SELECT
  7.     OBJECT_NAME(f.parent_object_id) AS TablaReferente,
  8.     COL_NAME(fc.parent_object_id, fc.parent_column_id) AS AtributoReferente,
  9.     OBJECT_NAME(f.referenced_object_id) AS TablaReferenciada,
  10.     COL_NAME(fc.referenced_object_id, fc.referenced_column_id) AS AtributoReferenciado,
  11.     f.name AS NombreConstraint
  12. FROM
  13.     sys.foreign_keys AS f
  14. INNER JOIN
  15.     sys.foreign_key_columns AS fc ON f.object_id = fc.constraint_object_id
  16. WHERE
  17.     OBJECT_NAME(f.referenced_object_id) = @NombreTabla
  18.     AND COL_NAME(fc.referenced_object_id, fc.referenced_column_id) = @NombreAtributo;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement