0

I want to store the result of select query from another table and set it as variable. Then i want to loop through the variable and use it in my loop for. The question is how i can store the result of select query from my table?

do $$
declare
 ids <data_type> := <SELECT DISTINCT (*) from a>;
begin 
for id in ids loop
insert
 into
 b (
 select from c where c.id in id
end loop;
end $$
1
  • 1
    You don't need a loop or PL/pgSQL for this. Commented Mar 17, 2021 at 7:56

1 Answer 1

1

Why not run a single query?

insert into b ( . . . )
    select . . .
    from c
    where c.id in (select id from a);

The . . . are for listing the columns/expressions for the insert.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.