复合触发器仅触发一次

Compound Trigger is Triggering Only Once

我有一个启用 ORDS 的架构,它接受批量 JSON 记录并将它们一条一条地拆分,然后将它们一条一条地插入到 UEM table。

我试图创建一个触发器,它获取最后插入的行的 ID 并使用该值插入另一个 table。问题是,下面的触发器只获取并插入最后插入的行的 id,而且它只插入 1 次。

更具体:

  1. ORDS 获得一个包含 4 条记录的批量 JSON 负载。

  2. POST 处理程序启动一个过程,该过程通过换行符拆分这 4 条记录,并立即将它们插入到 UEM table 的 CLOB 列中作为 4 个单独的行使用 “按级别连接”。还有自动创建和递增的 ID 列。

  3. 同时我还想获取这些行的 ID 并将其用于另一个 table 插入。我在下面创建了复合触发器,但是这个触发器只检索最后一条记录的 ID,并且只插入一行。

你认为它为什么会这样?最后,程序“插入”了 4 条记录。

CREATE OR REPLACE TRIGGER TEST_TRIGGER5
FOR INSERT ON UEM
COMPOUND TRIGGER
    lastid NUMBER;
    AFTER STATEMENT IS
        BEGIN
            SELECT MAX(ID) INTO lastid FROM UEM;
            INSERT INTO SPRINT1 (tenantid, usersessionid, newuser, totalerrorcount, userid) VALUES ('deneme', 'testsessionid', 'yes', lastid, 'asdasfqwqwe');
        END AFTER STATEMENT;
END TEST_TRIGGER5;

为什么?因为是语句级别触发器。如果你想让它为每一行触发,你 - 显然 - 使用具有

row level 触发器
for each row

条款。

inserts these to CLOB columns of UEM table as 4 separate rows by using "connect by level".

您有 1 个 INSERT 语句插入 4 行。

  1. In the parallel I also would like to get the ID of these rows and use it in another table insert. I've created the compound trigger below, but this trigger only retrieves the ID of the last record, and inserts only one row.

Why do you think it behaves like this? In the end, the procedure "inserted" 4 records.

它可能插入了 4 个 ROWS 但只有 1 个 STATEMENT 而您正在使用 AFTER STATEMENT 触发器。如果您希望它对每一行 运行,那么您需要使用行级触发器。

CREATE OR REPLACE TRIGGER TEST_TRIGGER5
AFTER INSERT ON UEM
  FOR EACH ROW
BEGIN
  INSERT INTO SPRINT1 (tenantid, usersessionid, newuser, totalerrorcount, userid)
  VALUES ('deneme', 'testsessionid', 'yes', :NEW.id, 'asdasfqwqwe');
END TEST_TRIGGER5;
/

db<>fiddle here