Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Trigger creation comparison then insert records

selvi mJun 28 2022

Hi Team,
I need to create trigger for below requirement after insert or update for each row.. i have created the trigger that not works fine ,,,
can you please help for this requirement for trigger...
Table Creation:
create table verify_activity (p_country varchar2(400),p_bank(200),v_nbr number(15) v_edate date,v_xdate date,v_user varchar3(25));
**Req:**New records came verify v_edate greater than v_xdate in existing records in table then only allow otherwise reject ..
p_country p_bank v_nbr v_edate v_xdate v_user
US, BAW, 12345, 2022-07-10, 2022-08-11, SNK
US, BAW, 12345, 2022-08-15, 2022-12-11, SNK --Allowed
US, BAW, 12345, 2022-08-09, 2022-08-11, SNK --not Allowed
code
CREATE OR REPLACE TRIGGER WWT.verify_activity_trg AFTER
INSERT OR UPDATE OF p_country,p_bank,V_NBR,V_EDATE,V_XDATE,V_USER
ON WWT.verify_activity
FOR EACH ROW
declare
VV_NBR number(10);
v_Edate date;
BEGIN
SELECT REPL_GROUP_NBR,EXP_DATE INTO V_NBR,v_Edate FROM UDT_MFC_OVERRIDE_INTL WHERE REPL_GROUP_NBR=:NEW.REPL_GROUP_NBR;
if v_xdate >:OLD.EXP_DATE THEN
insert into UDT_MFC_OVERRIDE_INTL values (:new.p_country,:new.p_bank,:new.V_NBR,:new.V_EDATE,:new.V_XDATE,:new.V_USER);
end if;
end;

Comments

Frank Kulash

Hi, @user-iap38
A FOR EACH ROW trigger on table verify_activity can't query verify_activity itself. If you need to read other rows from the same table, you can use an INSTEAD OF trigger.
Exactly how to do that depends on your exact requirements. What you posted already
**Req:**New records came verify v_edate greater than v_xdate in existing records in table then only allow otherwise reject ..
is pretty vague. When you say "new records" are you only talking about inserting new rows? If so, what do you want the trigger to do when you update old rows? What if you insert a new row today that should have been inserted last year, so the dates are older than dates already in the table?
Thanks for posting the CREATE TABLE statement. Post a few INSERT and UPDATE statements to be executed in order. Explain if each of them should be allowed or not, and if they are supposed to raise an error, explain why.

selvi m

As above comments short description below
while insert condition met that simply message records are inserted "Not inserted and if not messge to user

Frank Kulash

Hi, @user-iap38
As above comments short description below
while insert condition met that simply message records are inserted "Not inserted and if not messge to user
Sorry, that's still not clear. Answer the questions in the first reply, and post some actual INSERT and UPDATE statements, all of which would work without the trigger, but some of which fail because of the trigger.

1 - 3

Post Details

Added on Jun 28 2022
3 comments
173 views