Can I usemultiple return values of INSERT...RETURNING in another INSERT or multiples insert?

50 Views Asked by At

I need take the id of anexo an then the result id list insert in another table

with new_id as(INSERT INTO 
    clienteproveedor.anexo
    (
        codigo,
        estado,
        facturar,
        observaciones,
        servicioproductocontratadoid,
        formaspagoid  
    )  
      select           
        ServiciosPorAnexo.CodAnexo,
        ServiciosPorAnexo.cancelado,
         serviciosporanexo.facturacion,
        ServiciosPorAnexo.MotivosElim,
        ServiciosPorAnexo.codigo_servicio,
        ServiciosPorAnexo.FormaPago
        from clienteproveedor.serviciosporanexo 
        returning id);
1

There are 1 best solutions below

0
On

Yes you can do that, and you are close. Your problem being your returning clause is in the wrong place. Try:

with new_id(id) as
     (INSERT INTO 
        clienteproveedor.anexo
       (
        codigo,
        estado,
        facturar,
        observaciones,
        servicioproductocontratadoid,
        formaspagoid
       ) 
      retuning id 
    )  
    select           
      ServiciosPorAnexo.CodAnexo,
      ServiciosPorAnexo.cancelado,
      serviciosporanexo.facturacion,
      ServiciosPorAnexo.MotivosElim,
      ServiciosPorAnexo.codigo_servicio,
      ServiciosPorAnexo.FormaPago
     from clienteproveedor.serviciosporanexo 
    where ServiciosPorAnexo.id in 
          (select id from new_id);

NOTE: Not tested as I do not have table definitions nor example data.