I am not experienced in PL/SQL topics a lot, besides writing migration SQLs and some basic triggers. Because of that i will ask my question directly to have at least one start point into my trigger for table that i am expecting.
We have below tables :
VIB: Varchar
Locale: Varchar
Status : Varchar
Released : boolean
I need a trigger on D2C_EVENT_GENERATION_MASTER
table, the requirement is that, some system will give inserts to this table for "vib" column without LOCALE values for vibs . So our expectation is to calculate locales(find appropriate locales from release_table) before insert operation of that system, by asking RELEASE_TABLE
for appropriate locales that is existing for that VIB
, and released flag is 'true'.
CREATE OR REPLACE TRIGGER BL_D2C_EVENT_GENERATION_MASTER
BEFORE INSERT
ON D2C_EVENT_GENERATION_MASTER
FOR EACH ROW
DECLARE
l_locale varchar2(100);
BEGIN
if :NEW.locale is null then
for rec in (select locale from RELEASE_TABLE@pdpe.mch.bshg.com where vib=:NEW.vib AND released = 1)
loop
INSERT INTO d2c_event_generation_master (vib,locale) VALUES (:NEW.vib,rec.locale);
end loop;
DELETE FROM d2c_event_generation_master where vib=:NEW.vib AND locale is null;
end if;
END;
I am about to create this trigger, but only one exception occurs that i couldn't fix. When i insert a row with null locale value in table like this :
INSERT INTO D2C_EVENT_GENERATION_AP (vib) values ('GP200046');
It gets locales from other table but inserts three rows :
GP200046 fr-BE
GP200046 nl-BE
GP200046 null
I don't want to see 'null' here, i tried some execute immediate or some other staff, but couldn't find something.
Can you please help there ?
here is a trigger before insert to get value from the release table:
CREATE OR REPLACE TRIGGER BI_D2C_EVENT_GENERATION_MASTER
BEFORE INSERT
ON D2C_EVENT_GENERATION_MASTER
FOR EACH ROW
DECLARE
l_locale varchar2(100);
BEGIN
if :NEW.locale is null then
select locale INTO l_locale from release where vib=:NEW.vib;
:NEW.locale := l_locale;
end if;
END;
/
EDIT:
For the second solution I suggest creating a procedure:
CREATE OR REPLACE PROCEDURE MI_D2C_EVENT_GENERATION_MASTER (p_vib number, p_locale varchar2)
IS
l_vib number := p_vib;
l_locale varchar2(100) := p_locale;
BEGIN
MERGE INTO D2C_EVENT_GENERATION_MASTER event
USING (SELECT vib, locale from D2C_EVENT_GENERATION_MASTER) old_event
ON (event.vib = l_vib
and event.locale = l_locale)
WHEN MATCHED THEN
UPDATE SET event.STATUS = 'NEW'
WHERE event.STATUS != 'NEW'
WHEN NOT MATCHED THEN
INSERT (vib,locale,status) VALUES (l_vib, l_locale, 'STATUS')';
END;
/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.