Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

219
Vistas
Why I cannot bundle two sql commands with a ''with" statement in Postgresql?

Does with statement work with multiple sql commands? I have to delete entries in 2 tables. My sql is like this:

with tbd as (
 SELECT (row_value ->> 'id')::INTEGER 
 FROM public.row_history 
 where record_table = 'survey_storage' 
 and row_value ->> 'status' = 'Never Surveyed' 
 except
  (SELECT (row_value ->> 'id')::INTEGER 
   FROM public.row_history 
   where record_table = 'survey_storage' 
   and row_value ->> 'status' = 'Ready to Launch') 
  limit 1)
delete from row_history where (row_value ->> 'id')::INTEGER = ANY(ARRAY(select * from tbd)) 
delete from survey_storage where id = ANY(ARRAY(select * from tbd))

This is not working.Out of 2 delete statement if you comment out one, then the other one works. But together they do not even compile.Definitely I can write 2 sql scripts each with 1 delete - but I want to do this in one go. I get this error:

SQL Error [42601]: ERROR: syntax error at or near "delete"
                   Position: 410
                   ERROR: syntax error at or near "delete"
                   Position: 410
                   ERROR: syntax error at or near "delete"
                   Position: 410
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can do like below:

with tbd as (
 SELECT (row_value ->> 'id')::INTEGER 
 FROM public.row_history 
 where record_table = 'survey_storage' 
 and row_value ->> 'status' = 'Never Surveyed' 
 except
  (SELECT (row_value ->> 'id')::INTEGER 
   FROM public.row_history 
   where record_table = 'survey_storage' 
   and row_value ->> 'status' = 'Ready to Launch') 
  limit 1)
), cte2 as (
  delete from row_history 
     where (row_value ->> 'id')::INTEGER 
                        = ANY(ARRAY(select * from tbd))
)
delete from survey_storage
     where id = ANY(ARRAY(select * from tbd));
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda