簡體   English   中英

指定了4列的更新表,但是只有2列可用

[英]update table with 4 columns specified, but only 2 columns are available

我有一張表叫做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

有什么問題,我該如何解決?

values子句的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本質上是相同的。 第一個返回具有匿名復合類型的列,該復合類型具有兩個字段。 第二個返回表中的兩列。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM