简体   繁体   English

仅在触发之前运行一次以插入多行PL / SQL

[英]run before trigger only once to insert multiple rows PL/SQL


I have a few parameterization tables, where i need to add history version via two columns VALID_FROM and VALID_TO. 我有一些参数化表,需要在其中通过两列VALID_FROM和VALID_TO添加历史记录版本。 Before inserting rows i have two precondition: 在插入行之前,我有两个前提条件:

1) every record, which is in table get new value in column VALID_TO (sysday - 1) 1)表中的每个记录在VALID_TO列中获得新值(sysday-1)
2) if exists some records in a table which have same date as sysdate (DD.MM.YYYY), they must be deleted. 2)如果表中存在一些与sysdate(DD.MM.YYYY)具有相同日期的记录,则必须将其删除。

table before inserting rows 插入行之前的表格

columnA VALID_FROM  VALID_TO
row1    1.1.2016    31.12.2999
row2    1.1.2016    31.12.2999
row3    3.1.2016    31.12.2999

table after inserting rows 插入行后的表格

columnA VALID_FROM  VALID_TO
row1    1.1.2016    24.6.2018
row2    1.1.2016    24.6.2018
row3    3.1.2016    24.6.2018
row1    25.6.2018   31.12.2999
row2    25.6.2018   31.12.2999
row3    25.6.2018   31.12.2999
row4    25.6.2018   31.12.2999

These two steps should be finished before inserting new rows and should be automatic. 这两个步骤应该在插入新行之前完成,并且应该是自动的。 This is the assignment of the task I received. 这是我收到的任务的任务。 I cant change it. 我不能改变它。

I tried to create before insert trigger with global temporary table to avoid ora mutating error, but its not working, because every inserted row is deleted and dates in columns VALID_FROM and VALID to are not correct too. 我尝试使用全局临时表在插入触发器之前创建触发器,以避免ora突变错误,但是它不起作用,因为删除了每个插入的行,并且VALID_FROM和VALID列中的日期也不正确。 So I need to check old records in table only once and then insert all rows without checking 所以我只需要检查一次表中的旧记录,然后插入所有行而不检查

My code: 我的代码:

CREATE OR REPLACE TRIGGER CHANGE_DATE_PARM_TABLE_MUT2
 after INSERT on TMP_GR_PARM_IDP_IC_CUST
 FOR each ROW
 BEGIN
   --if exists record which have todas date, then delete
  delete from GR_PARM_IDP_IC_CUST1 where DT_VALID_FROM = get_sysdate;
  --change date in VALID_TO for old records
  update GR_PARM_IDP_IC_CUST1 a set
  DT_VALID_TO = TO_CHAR(SYSDATE - 1, 'DD.MM.YYYY')
  where DT_VALID_TO = '31.12.2999';
 --insert records from temporary table into normal parametrization table
  insert into GR_PARM_IDP_IC_CUST1 values (:new.RELATION, :new.DT_VALID_FROM, :new.DT_VALID_TO);
END;

Its is possible solve this problem with trigger? 它可以用触发器解决这个问题吗? If yes, can someone help me? 如果是,有人可以帮助我吗?

Your code looks ok, just execute it BEFORE INSERT instead of AFTER INSERT . 您的代码看起来不错,只需BEFORE INSERT而不是AFTER INSERT And I don't see why you need a global temporary table. 而且我不明白为什么您需要一个全局临时表。 Does the following trigger fulfill your requirements? 以下触发器是否满足您的要求?

CREATE OR REPLACE TRIGGER ti_mytable 
  BEFORE INSERT ON mytable FOR EACH ROW
BEGIN
  DELETE FROM mytable WHERE valid_from = TRUNC(SYSDATE);
  UPDATE mytable SET valid_to = TRUNC(SYSDATE - 1);
  :new.valid_from := TRUNC(SYSDATE);
  :new.valid_to := DATE '2999-12-31';
END ti_mytable;
/

SELECT * FROM mytable;
COLA  VALID_FROM   VALID_TO
1     01.01.2016   31.12.2999
2     01.01.2016   31.12.2999
3     03.01.2016   31.12.2999

INSERT INTO mytable (cola) VALUES (4);
1 row inserted.
SELECT * FROM mytable;
COLA  VALID_FROM   VALID_TO
1     01.01.2016   24.06.2018
2     01.01.2016   24.06.2018
3     03.01.2016   24.06.2018
4     25.06.2018   31.12.2999

Furthermore, I'd recommend to store dates always as datatype DATE and never as a string / datatype VARCHAR2 . 此外,我建议始终将日期存储为数据类型DATEVARCHAR2存储为字符串/数据类型VARCHAR2

无法通过TRIGGER触发。

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

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