2

I've searched around and couldn't find a solution for my question - I want to insert multiple of the same values in a table?

For example, I'm using Asyncpg for python, I would do something like this:

for i in range(5):
  await execute("INSERT INTO table (column_a, column_b) VALUES (1, 2)")

while using a sequence to create unique "ID's" for each row. I understand you can use multiple values, but I want to make it so it's an arbitrary amount, how would I go about doing this?

3
  • I can't really understand what you want to do. What would be the expected result? 5 rows with the same values (1, 2)? Commented May 1, 2020 at 21:45
  • Yes, basically inserting a table with 5 rows with the same values. Commented May 1, 2020 at 21:46
  • Remove the WHERE section. Use: INSERT INTO table (column_a, column_b) VALUES (1, 2) Commented May 1, 2020 at 21:46

1 Answer 1

2

In Postgres, you can insert 5 rows at once with a single query using generate_series():

insert into mytable (column_a, column_b) 
select 1, 2
from generate_series(1, 5);
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.