Hana Studio(SQL脚本)触发器上的条件更新

时间:2018-09-12 14:39:27

标签: sap hana sql-scripts hana-sql-script hana-studio

我有两个示例表:

    TABLE1:
      ID    | COLUMN_B| COLUMN_C
   _________|_________|_________
       1    |    0    |    1
    ________|_________|_________
       2    |    0    |    1
    ________|_________|_________
       3    |    0    |    1


    TABLE2:
      ID    | COLUMN_E| COLUMN_F
    ________|_________|________
       1    |    Y    |    X
    ________|_________|_________
       2    |    Y    |    X
    ________|_________|_________
       3    |    Y    |    X

我想做的是,当表1中的B列更新为特定值(例如100)时,使用SQL脚本在Hana Studio上编写触发器以更新表2中的E列。 表1中的行在表2中带有第一列(ID)引用。

我编写了以下代码:

create trigger "UPDATE_TABLES"
    after update of "COLUMN_B" on "TABLE1"
    referencing new row as new, old row as old
    for each row 
begin
    update "TABLE2"
    set "COLUMN_E" = 'my new value'
    where :old."COLUMN_B" = '100' and "TABLE1"."ID" = :old."ID";
end;

当我将第1行的TABLE1.COLUMN_B设置为100时,我希望代码将第1行的TABLE2.COLUMN_E更改为“我的新值”,但是什么也没有发生。有人可以指出我在这里究竟在做什么错吗?

2 个答案:

答案 0 :(得分:1)

我猜触发代码中的UPDATE语句应该稍有不同,如下所示

create trigger "UPDATE_TABLES"
    after update of "COLUMN_B" on "TABLE1"
    referencing new row as new, old row as old
    for each row 
begin
    update "TABLE2"
    set "COLUMN_E" = 'my new value'
    where :new."COLUMN_B" = '100' 
    and TABLE2.ID = :old."ID";
end;

否则,在创建触发器时出现语法错误 您能验证一下吗?

答案 1 :(得分:0)

菜鸟错误。我希望column_e值更改column_b中值的更改,但实际上我是在跟踪column_b中的旧值。更正为:

create trigger "UPDATE_TABLES"
    after update of "COLUMN_B" on "TABLE1"
    referencing new row as new, old row as old
    for each row 
begin
    update "TABLE2"
    set "COLUMN_E" = 'my new value'
    where :new."COLUMN_B" = '100' and "TABLE1"."ID" = :old."ID";
end;

成功了!