sql 在 teradata 中更新

sql update in teradata

table_1 
(
    c1 int
    ,c2 int
    ,c3 int
);

insert into table_1 values (1,2,3);

update table_1
    set c2 = c2 + 5  --c2's value suppose to be 7 now
   ,c3 = c2 + c1;    --c3's value suppose to be 7 + 1 = 8 now

select c1, c2, c3 from table_1;

result: 1, 7, 3;

结果应该是:1、7、8。

但是c3的值还是3,没做任何改变

对不起我的英语。

UPDATE 语句使用值 "before" UPDATE,即

c2 = c2 + 5  => 2 + 5 = 7
but
c3 = c2 + c1 => 1 + 2 = 3

这样执行:

SQL>create table table_1 ( c1 int ,c2 int ,c3 int );
SQL>insert into table_1 values (1,2,3);
SQL>update table_1
SQL&    set c2 = c2 + 5  --c2's value is set to 7 now
SQL&   ,c3 = c2 + c1;    --c3's value is set to 2 + 1 = 3 now

                  1 row updated

SQL>select c1, c2, c3 from table_1;
         c1          c2          c3
=========== =========== ===========
          1           7           3

                  1 row found

您甚至可以在列之间移动值:

SQL>update table_1 set c3 = c2, c2 = c1, c1 = c3;

                  1 row updated

SQL>select c1, c2, c3 from table_1;
         c1          c2          c3
=========== =========== ===========
          3           1           7

                  1 row found

要逐个更新列,请使用单独的 UPDATE 语句:

SQL>create table table_1 ( c1 int ,c2 int ,c3 int );
SQL>insert into table_1 values (1,2,3);
SQL>update table_1
SQL&    set c2 = c2 + 5;  -- c2's value suppose to be 7 now

                  1 row updated

SQL>update table_1
SQL&    set c3 = c2 + c1;  -- c3's value suppose to be 7 + 1 = 8 now

                  1 row updated

SQL>select * from table_1;
         c1          c2          c3
=========== =========== ===========
          1           7           8

                  1 row found