Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

postgresql insert multiple rows - fail

Tags:

postgresql

I'm using PostgreSQL 8.1.11.

And I'm losing my mind. Why can I not use a basic SQL statement as INSERT?

I provide:

INSERT INTO the_leads_details ( id, lead_id, question_id, i_value, c_value ) VALUES
( 1, 1, 1, NULL, '4500' ), ( 2, 1, 2,    1, NULL );

                         ^ this comma is a problem

What I am missing? This seems like a basic SQL INSERT statement to insert multiple rows. Is my problem related to my PostgreSQL version?

I am inserting a lot of rows and I am looking to optimize INSERT multiple rows instead of placing several INSERTs.

like image 661
bensiu Avatar asked Aug 22 '10 11:08

bensiu


People also ask

How DO I INSERT multiple rows at the same time in SQL?

INSERT-SELECT-UNION query to insert multiple records Thus, we can use INSERT-SELECT-UNION query to insert data into multiple rows of the table. The SQL UNION query helps to select all the data that has been enclosed by the SELECT query through the INSERT statement.

Can we INSERT multiple rows single INSERT statement?

Answer. Yes, instead of inserting each row in a separate INSERT statement, you can actually insert multiple rows in a single statement. To do this, you can list the values for each row separated by commas, following the VALUES clause of the statement.

What is on CONFLICT in PostgreSQL?

ON CONFLICT DO NOTHING simply avoids inserting a row as its alternative action. ON CONFLICT DO UPDATE updates the existing row that conflicts with the row proposed for insertion as its alternative action. conflict_target can perform unique index inference.

How to INSERT in PostgreSQL?

First thing to do is specify the table name followed by the columns where you want to insert the data or rows. Secondly, you must list a comma-separated value list after the VALUES clause. The value list must be in the same order as the columns list specified after the table name.


2 Answers

Multi-row INSERT syntax is not supported in PostgreSQL 8.1, you need to upgrade to 8.2 or newer (and if you upgrade today, you really should upgrade to 8.4, not 8.2!)

Another reason is, as Frank mentioned in a comment, that version 8.1 will go end-of-life in November, so it's really time to start investigating upgrading.

like image 130
Magnus Hagander Avatar answered Sep 30 '22 06:09

Magnus Hagander


I'm not sure Postgresl 8.1 supports multiple rows in VALUES. The syntax is:

INSERT INTO table [ ( column [, ...] ) ]
    { DEFAULT VALUES | VALUES ( { expression | DEFAULT } [, ...] ) | query }

http://www.postgresql.org/docs/8.1/static/sql-insert.html

like image 35
Edmund Avatar answered Sep 30 '22 04:09

Edmund