Tabla de Contenidos

MERGE

La sentencia merge permite seleccionar datos de una tabla y insertar, borrar o actualizar otra tabla en base a condiciones de coincidencia.

Es de lo más útil para casos del estilo, Si está lo actualizas y si no lo mentes…

SINTAXIS

MERGE INTO tabla_destino d
  USING
     tabla_origen s
  ON 
     (d.campo_igual = s.campo_igual) 
  WHEN MATCHED THEN
               -- SI COINCIDE, ES DECIR, SI ESTABA ANTES.... 
               UPDATE SET d.campo_a_actualizar = s.campo_que_tu_quieras
  WHEN NOT MATCHED THEN
	-- SI NO COINCIDE, ES DECIR, SI NO ESTABA ANTES.... 
	INSERT (d.campo1, d.campo2, d.campoN)
              VALUES (s.campo1, s.campo2, s.campoN);

EJEMPLO

merge into 
  tabla1 d
using
  (select produ2, ge3, cd4, ti5, PREVI0006 
  from tabla2
  where ti5 >= hoy ) s
on 
  (  (d.produ2 = s.produ2) 
      and (d.ge3 = s.ge3)   
      and (d.cd4 = s.cd4)
      and (d.ti5 = s.ti5)
      )
when     matched then 
  update set d.ARCH0004 = s.PREVI0006
when not matched then 
 insert (produ2, ge3,cd4,ti5, ARCH0004)
 values (s.produ2, s.ge3, s.cd4, s.ti5, s.PREVI0006);
 
plsql/8.txt · Última modificación: 2008/05/13 10:00 (editor externo)
 
Excepto donde se indique lo contrario, el contenido de esta wiki se autoriza bajo la siguiente licencia:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki