简体   繁体   English

如何更新 SAP HANA 触发器中的同一个表

[英]How to update the same table in SAP HANA Triggers

I am trying to write a trigger in SAP HANA to update a field of a table when new records are being inserted to that table.我正在尝试在 SAP HANA 中编写一个触发器,以便在将新记录插入到该表时更新该表的字段。 Following is a sample trigger that I have written.以下是我编写的示例触发器。

CREATE TRIGGER SAMPLE
AFTER INSERT ON TARGET_TABLE
REFERENCING NEW ROW NEW_ROW
FOR EACH ROW
BEGIN
    UPDATE TARGET_TABLE SET VALID_FROM='2018-02-01' WHERE ITEM=:NEW_ROW.ITEM
END

When I try this, I get the error, Modification of subject table in trigger not allowed .当我尝试这个时,我收到错误, Modification of subject table in trigger not allowed

Is there a way by which I can achieve this?有什么方法可以实现这一目标吗?

https://archive.sap.com/discussions/thread/3651854 suggests to use the transition variable NEW_ROW , appreciate if a code sample can be provided. https://archive.sap.com/discussions/thread/3651854建议使用转换变量NEW_ROW ,如果可以提供代码示例,不胜感激。

You don't actually need to create trigger for your requirement as I can see from your post (of course if it is only updating a date field)正如我从您的帖子中看到的那样,您实际上不需要为您的要求创建触发器(当然,如果它只是更新日期字段)

You can define the VALID_FROM column with a DEFAULT value您可以使用 DEFAULT 值定义 VALID_FROM 列

For example,例如,

    Create Column Table DefaultColumnTable (
     Id int,
     Code varchar(5),
     VALID_FROM date default '2018-02-01'
    )

So whenever a new row is inserted, unless an alternative value is stated the valid_from column will be populated with default date specified in the DDL command above.因此,每当插入新行时,除非声明了替代值,否则 valid_from 列将使用上面 DDL 命令中指定的默认日期填充。

The users can change the valid_from field value without any problem用户可以毫无问题地更改 valid_from 字段值

@Kalpa, maybe you can use BEFORE INSERT Trigger Please check following sample @Kalpa,也许您可​​以使用 BEFORE INSERT 触发器 请检查以下示例

create trigger TriggerTable_B_INS BEFORE INSERT on TriggerTable
REFERENCING NEW ROW mynewrow
FOR EACH ROW
begin

declare lv_d date;
lv_d := '20180201';

mynewrow.VALID_FROM = :lv_d;

end;

You set only the new row columns before Insert command executes over the target table.在插入命令对目标表执行之前,您只设置新的行列。 You don't explicitly execute an INSERT command, just set new values for new row columns.您没有显式执行 INSERT 命令,只需为新行列设置新值。 That's all就这样

I hope it helps我希望它有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM