Buscar contenidos

jueves, 30 de abril de 2020

SQL Merge Script



https://www.pigeonsql.com/post/dynamic-sql-merge-script-for-generate-merge-statement


---------INICIO|Tabla|Pais---------
--IdPais Descripcion CodPais
MERGE INTO dbo.Pais AS Target 
USING (VALUES 
  ('Costa Rica', 'CRI')
  ,('Guatemala', 'GTMAA')

AS Source (Descripcion, CodPais) 
ON (Target.CodPais = Source.CodPais)
-- Actualizar registros
WHEN MATCHED THEN 
UPDATE SET Descripcion = Source.Descripcion, CodPais = Source.CodPais
  
-- Insertar nuevos registros
WHEN NOT MATCHED BY TARGET THEN 
INSERT (Descripcion, CodPais) 
VALUES (Descripcion, CodPais) 
-- delete rows that are in the target but not the source 
WHEN NOT MATCHED BY SOURCE THEN 
DELETE;
---------FIN|Tabla|Pais---------

No hay comentarios:

Publicar un comentario