Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to insert multiple rows in a single PostgreSQL query? #730

Answered by kyleconroy
namndev asked this question in Q&A
Discussion options

Hi @kyleconroy,

The first, I really like this repo. Thank for your effort,

Now, I want inserting multiple rows in a single query like:

INSERT INTO public."Item" ("Id", name)
VALUES ('1', 'name1'),
 ('2', 'name2'),
 ('3','name3')

I don't know how to write in sqlc?

You must be logged in to vote

Sadly, this isn't currently supported. Your unnest solution is interesting, I wonder how well it performs for a large number of records. How many records are your trying to insert? PostgreSQL has a guide for inserting large amounts of data. If you aren't inserting many records, I'd just use a transaction. Sorry that there isn't a better answer.

This has also been discussed in #216 and #218, which you may find helpful.

Replies: 2 comments 1 reply

Comment options

Hi,
I came up with a solution to the above problem:

-- name: AddItems :execrows
INSERT INTO Item (Id, name) VALUES (UNNEST(@ids::INT8[]) , UNNEST(@names::varchar[]));

However, with where clause like:

SELECT * FROM item WHERE (id = '1' AND name = 'name1') OR (id = '2' AND name = 'name2') OR (id = '3' AND name = 'name3')

I don't know how to write in sqlc?

You must be logged in to vote
0 replies
Comment options

Sadly, this isn't currently supported. Your unnest solution is interesting, I wonder how well it performs for a large number of records. How many records are your trying to insert? PostgreSQL has a guide for inserting large amounts of data. If you aren't inserting many records, I'd just use a transaction. Sorry that there isn't a better answer.

This has also been discussed in #216 and #218, which you may find helpful.

You must be logged in to vote
1 reply
Comment options

Thank @kyleconroy .

Answer selected by kyleconroy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
Converted from issue

This discussion was converted from issue #730 on October 19, 2020 06:43.

AltStyle によって変換されたページ (->オリジナル) /