postgresql 插入多行 - 失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3541336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-20 00:15:40  来源:igfitidea点击:

postgresql insert multiple rows - fail

postgresql

提问by bensiu

I'm using PostgreSQL 8.1.11.

我正在使用 PostgreSQL 8.1.11。

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

我正在失去理智。为什么我不能使用基本的 SQL 语句作为 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?

我缺少什么?这似乎是插入多行的基本 SQL INSERT 语句。我的问题与我的 PostgreSQL 版本有关吗?

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

我正在插入很多行,我希望优化 INSERT 多行而不是放置多个 INSERT。

回答by Magnus Hagander

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!)

PostgreSQL 8.1 不支持多行 INSERT 语法,你需要升级到 8.2 或更高版本(如果你今天升级,你真的应该升级到 8.4,而不是 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 reallytime to start investigating upgrading.

另一个原因是,弗兰克在评论中提到,该版本8.1将去结束生命十一月,所以它的真是时候开始调查升级。

回答by Edmund

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

我不确定 Postgresl 8.1 是否支持 VALUES 中的多行。语法是:

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

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

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

回答by frogstarr78

I know it's an old thread but, this will work:

我知道这是一个旧线程,但是,这会起作用:

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

回答by vulkanino

The syntax is correct, are you sure that the problem is in the comma?

语法正确,您确定问题出在逗号上吗?