1

I have an array string [1,2,3,4]

Here is my sql

INSERT INTO account(account_id, parent_id) VALUES (1, 10);
INSERT INTO account(account_id, parent_id) VALUES (2, 10);
INSERT INTO account(account_id, parent_id) VALUES (3, 10);
INSERT INTO account(account_id, parent_id) VALUES (4, 10);

how to insert using postgresql forloop function?

Thank you.

1 Answer 1

2

No need for a loop:

insert into account(account_id, parent_id)
select t.id, 10
from unnest(array[1,2,3,4]) as t(id);
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.