使用合并

时间:2019-04-29 18:21:33

标签: oracle plsql triggers database-trigger scd

在我生命中,我无法获得以下查询,因为它本质上是对以下票证的修改: Trigger with Merge statement in oracle

CREATE OR REPLACE TRIGGER TABLE_UPDATE
  AFTER INSERT OR UPDATE ON DIM_TABLE
FOR EACH ROW
DECLARE
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
IF INSERTING OR UPDATING
THEN
    MERGE INTO DIM_TABLE T_1
          USING( SELECT COL_1, max(VALID_FROM) AS LAST_DATE FROM 
DIM_TABLE
          GROUP BY COL_1) T_2
          ON (T_1. COL_1= T_2. COL_1)

    WHEN NOT MATCHED THEN INSERT (T_1. VALID_FROM) VALUES(SYSDATE)

    WHEN MATCHED THEN
      UPDATE
      SET T_1.VALID_UNTIL = T_2.LAST_DATE
      WHERE T_1. VALID_UNTIL is null 
      AND T_1. VALID_FROM <> T_2.LAST_DATE;
   COMMIT;
 END IF; 
END;

所需的功能:将一个值输入数据库(COL_1)。 如果该列不匹配,则将VALID_FROM日期输入为sysdate。如果存在匹配项,则脚本应更新上一行的VALID_UNTIL值(由于它仍然有效,所以直到现在为空)。

我不断收到以下错误: 死锁和最大递归级别超过(50)

2 个答案:

答案 0 :(得分:0)

我真的很难满足您的要求。在我看来,您似乎希望VALID_FROMsysdate或每个COL_1的最大值。

我将通过使用:NEW来操纵当前行来完成

set linesize 250
drop table dim_Table;
create table dim_table (col_1 varchar2(50), valid_from timestamp);

CREATE OR REPLACE TRIGGER TABLE_UPDATE
  before INSERT OR UPDATE ON DIM_TABLE
FOR EACH ROW
DECLARE
  l_valid_from date;
BEGIN
  select max(valid_from) 
    into l_valid_from
   from  dim_table t1
   where t1.col_1 = :new.col_1;
   if l_valid_from is null then
     :new.valid_from := sysdate;
   else
     :new.valid_from := l_valid_from;
   end if;
END;

insert into dim_table (col_1) values ('TEST1');
select * from dim_table;
exec dbms_lock.sleep(1);
insert into dim_table (col_1) values ('TEST1');
select * from dim_table;
exec dbms_lock.sleep(1);
insert into dim_table (col_1) values ('TEST2');
select * from dim_table;
exec dbms_lock.sleep(1);
insert into dim_table (col_1) values ('TEST1');
select * from dim_table;
exec dbms_lock.sleep(1);
insert into dim_table (col_1) values ('TEST2');
select * from dim_table;

输出:

Table dropped.
Table created.
Trigger created.
1 row created.

COL_1                                              VALID_FROM                                        
-------------------------------------------------- --------------------------------------------------
TEST1                                              30/04/2019 09:13:10.000000                        
1 row selected.
 PL/SQL procedure successfully completed.
1 row created.

COL_1                                              VALID_FROM                                        
-------------------------------------------------- --------------------------------------------------
TEST1                                              30/04/2019 09:13:10.000000                        
TEST1                                              30/04/2019 09:13:10.000000                        

2 rows selected.
 PL/SQL procedure successfully completed.
1 row created.

COL_1                                              VALID_FROM                                        
-------------------------------------------------- --------------------------------------------------
TEST1                                              30/04/2019 09:13:10.000000                        
TEST1                                              30/04/2019 09:13:10.000000                        
TEST2                                              30/04/2019 09:13:12.000000                        

3 rows selected.
 PL/SQL procedure successfully completed.
1 row created.

COL_1                                              VALID_FROM                                        
-------------------------------------------------- --------------------------------------------------
TEST1                                              30/04/2019 09:13:10.000000                        
TEST1                                              30/04/2019 09:13:10.000000                        
TEST2                                              30/04/2019 09:13:12.000000                        
TEST1                                              30/04/2019 09:13:10.000000                        

4 rows selected.
 PL/SQL procedure successfully completed.
1 row created.

COL_1                                              VALID_FROM                                        
-------------------------------------------------- --------------------------------------------------
TEST1                                              30/04/2019 09:13:10.000000                        
TEST1                                              30/04/2019 09:13:10.000000                        
TEST2                                              30/04/2019 09:13:12.000000                        
TEST1                                              30/04/2019 09:13:10.000000                        
TEST2                                              30/04/2019 09:13:12.000000                        

5 rows selected.

答案 1 :(得分:0)

谢谢@ hol,@ Alex Poole,我想我破解了。 每当添加任何内容时,我都会使用触发器将数据插入VALID_FROM:

 CREATE TABLE "DIM_TEST" (  "COL_1" VARCHAR2(20 BYTE), "VALID_UNTIL" TIMESTAMP (6), 
"DIM_TEST_PK" NUMBER, "VALID_FROM" TIMESTAMP (6) DEFAULT NULL) 

  create or replace TRIGGER T_DIM_TEST_DATE_INSERT
  BEFORE INSERT ON DIM_TEST
  FOR EACH ROW
  BEGIN
    :new.VALID_FROM := sysdate;
  END;

对于添加重复项时更新上一行VALID_FROM的部分,我具有以下触发器:

 create or replace TRIGGER TABLE_UPDATE
 after INSERT or update
 ON DIM_TEST
 FOR EACH ROW
 DECLARE
 PRAGMA AUTONOMOUS_TRANSACTION;
 BEGIN
 IF INSERTING
 THEN
    MERGE INTO DIM_TEST T_1
    USING (select :new.COL_1 from DUAL) T_2
      ON (T_1.COL_1 = :new.COL_1)

WHEN MATCHED THEN
  UPDATE
  SET T_1.VALID_UNTIL = (SYSDATE)
  WHERE T_1. VALID_UNTIL is null;
  --AND T_1. VALID_FROM <> (SELECT max(VALID_FROM) FROM DIM_TEST);
 END IF;  
COMMIT;
END;

唯一的缺点是,如果将现有行更新为重复行,则由于IF INSERTING子句而不会触发第二个触发器...当我将其更改为 如果插入或更新,它将生成一个无限循环...但是我只能将用户限制为插入,因此这不成问题。