You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PostgreSQL插入多行失败:所有值存入同一行的问题求助

解决PostgreSQL多行插入语法错误问题

你的插入语句语法有误,PostgreSQL将每组嵌套括号解析成了复合类型值,导致所有数据被塞进单行的三个列中。

错误原因分析

原语句中:

insert into "DBSTest".Test (column1, column2, column3) 
values (
('test1a','test1b'),
('test2a','test2b'),
('test3a','test3b')
);

外层括号内的('test1a','test1b')被视为单个列值(复合类型),对应column1;同理另外两组分别对应column2和column3,因此最终只插入了一行,每个列存储的是复合类型的文本形式。

正确的多行插入写法

方法1:标准多行VALUES语法

每个行数据用单独的括号包裹,逗号分隔:

insert into "DBSTest".Test (column1, column2, column3) 
values 
('test1a', 'test2a', 'test3a'),
('test1b', 'test2b', 'test3b');

方法2:使用UNION ALL批量插入

适合需要从查询结果或复杂逻辑生成数据的场景:

insert into "DBSTest".Test (column1, column2, column3)
select 'test1a', 'test2a', 'test3a'
union all
select 'test1b', 'test2b', 'test3b';

执行后的正确结果

column1column2column3
test1atest2atest3a
test1btest2btest3b

内容的提问来源于stack exchange,提问作者Daniel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 11:46:01