Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

213
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!