Casting NULL type when updating multiple rows

前端 未结 3 1897
醉话见心
醉话见心 2020-11-29 09:38

I have a problem when I try to update many rows at the same time.

Here is the table and query I use (simplified for better reading):

table<

3条回答
  •  一生所求
    2020-11-29 10:07

    Your script will create a temporary table from foo. It will have the same data types as foo. Use an impossible condition so it is empty:

    select x, y, pkid
    into temp t
    from foo
    where pkid = -1
    

    Make your script to insert into it:

    insert into t (x, y, pkid) values
    (null, 20, 1),
    (null, 50, 2)
    

    Now update from it:

    update foo 
    set x=t.x, y=t.y 
    from t
    where foo.pkid=t.pkid
    

    Finally drop it:

    drop table t
    

提交回复
热议问题