我有一个名为test的表,它有4列:
id INT
v_out INT
v_in INT
label CHARACTER
我正在尝试使用以下查询更新表:
String sql = "
update
test
set
v_out = temp.outV
, v_in = temp.inV
, label = temp.label
from (
values(
(1,234,235,'abc')
,(2,234,5585,'def')
)
) as temp (e_id, outV, inV, label)
where
id = temp.e_id
";
当我执行它时,我收到错误:
org.postgresql.util.PSQLException:错误:
table "temp" has 2 columns available but 4 columns specified
问题是什么,我该如何解决?
答案 0 :(得分:2)
values
子句的值不得括在括号中:
values (
(1,234,235,'abc'), (2,234,5585,'def')
)
创建一个包含两列的单行。每列都是匿名"记录"有4个字段。
你想要的是:
from (
values
(1,234,235,'abc'),
(2,234,5585,'def')
) as temp (e_id, outV, inV, label)
SQLFiddle显示差异:http://sqlfiddle.com/#!15/d41d8/2763
记录了这种行为,但很难找到:
http://www.postgresql.org/docs/current/static/rowtypes.html#AEN7362
它与select (col1, col2) from some_table
与select col1, col2 from some_table
基本相同。第一个返回一列具有两个字段的匿名复合类型。第二个从表中返回两列。