How to handle exception within trigger - oracle

I want to add exception handling within trigger.
I created Trigger as below.
I want to add exception handling within that. So that my trigger never failed if there are any invalid condition.
CREATE OR REPLACE TRIGGER system_notification_audit
AFTER
INSERT OR
UPDATE
on system_notification
FOR EACH ROW
begin
insert into system_notification_log
select :NEW.ID , :NEW.NAME, :NEW.Description, :NEW.PREFERENCE, :NEW.FREQUENCY,
:NEW.IS_HIGH, :NEW.IS_REQUIRED, :NEW.UPDATED_BY, :NEW.UPDATED_DATE
from dual
where :OLD.PREFERENCE <> :NEW.PREFERENCE
OR :OLD.FREQUENCY <> :NEW.FREQUENCY OR :NEW.IS_HIGH <> :OLD.IS_HIGH OR :NEW.IS_REQUIRED <> :OLD.IS_REQUIRED;
END;

Simply Ignoring an Exception isn't always a good way to design your program. At least log the errors somewhere for later observation.
Note that your INSERT statement can be rewritten using a simple insert ( without a select ) and an IF condition.
CREATE OR REPLACE TRIGGER system_notification_audit AFTER
INSERT OR UPDATE ON system_notification
FOR EACH ROW
BEGIN
IF
:old.preference <>:new.preference OR :old.frequency <>:new.frequency
OR :new.is_high <>:old.is_high OR :new.is_required <>:old.is_required
THEN
INSERT INTO system_notification_log (
id,
name,
description,
preference,
frequency,
is_high,
is_required,
updated_by,
updated_date
) VALUES (
:new.id,
:new.name,
:new.description,
:new.preference,
:new.frequency,
:new.is_high,
:new.is_required,
:new.updated_by,
:new.updated_date
);
END IF;
EXCEPTION WHEN OTHERS THEN
pr_trigger_logs(trig_name => 'system_notification_audit',
err_msg => DBMS_UTILITY.FORMAT_ERROR_BACKTRACE());
-- calling an error logging procedure.
END;
/
I won't give you the definition of procedure pr_trigger_logs. As an exercise, I'll let you come up with it!.

Related

How to capture errors inside trigger

I want to create trigger which should capture all error whatever it happens inside the trigger.
And it should store in some log table.
Here my challange is how to capture the error occur in DML statments.
THat DML statments error captured in log with respective table name along with column name and error message.
It should also caputre all other errors like no_data_found with exact line number.
Please look at my code below.
If any changes need tell us.
create or replace TRIGGER user_name.sample_trg
AFTER UPDATE ON user_name.transaction_tb
FOR EACH ROW
DECLARE
variable_ln number;
l_err varcha2(4000);
BEGIN
select column_value
into variable_ln
from tb1
where colum_1 = :NEW.colum_1
IF UPDATING THEN
INSERT
INTO hisotry_tb
(
column1,
column2,
column3,
column4,
)
VALUES
(
:NEW.column1,
:NEW.column2,
:NEW.column3,
:NEW.column4,
);
END IF;
IF INSERTING THEN
INSERT
INTO hisotry_tb
(
column5,
column6,
column7,
column8,
)
VALUES
(
:NEW.column5,
:NEW.column6,
:NEW.column7,
:NEW.column8,
);
END IF;
EXCEPTION
WHEN OTHERS THEN
l_err := DBMS_UTILITY.FORMAT_ERROR_STACK || DBMS_UTILITY.FORMAT_ERROR_BACKTRACE;
INSERT INTO log (erro_msg, trigger_name, column_name)
VALUES (l_err, 'sample_trg', '?');
DBMS_OUTPUT.put_line (l_err);
END;

Update with "with data as" clause and regex do not commit. Why?

Sorry for the delay! I've taken a workload greater then i can handle these couple weeks.
Ok, lets clarify things up!
There is a proprietary software running on top of it and
I do not have de ability to change this software! Actually it allows me to do a few things.
In this specific case I can not create a relation 1>N, I can not create a new table! What I do can is create fields.
So, how do i customize things? Through the database! Using triggers, functions and procedures. I know its a bad "work around" but it's what i got and works flawlessly 99% of times. Any exception my code throws the application shows on the screen!
The problem is actually the update not working.
You ask:
Why you're looping when you're forcing there to only be one iteration (unless P_QTDLINHAS can be < 1 I suppose?)
R: Couse the user can select multiple lines in the application but I dont want them to do it. So a throw an error on the screen.
Why you have nested begin/end blocks.
R: Couse I may have to throw exceptions, I got used to write begin Statements Exception some message end.
Sample data:
CREATE TABLE SAMPLE_DATA
(
PKNUMBER NUMBER NOT NULL
, DESCRIPTION VARCHAR2(20)
, GROUPTOCHANGE VARCHAR2(100)
, STATUS VARCHAR2(1 BYTE)
, CONSTRAINT SAMPLE_DATA_PK PRIMARY KEY
(
PKNUMBER
)
ENABLE
);
INSERT INTO sample_data VALUES (1000,'ORDER1',NULL,NULL);
INSERT INTO sample_data VALUES (2000,'ORDER2',NULL,NULL);
INSERT INTO sample_data VALUES (3000,'ORDER3',NULL,NULL);
INSERT INTO sample_data VALUES (4000,'ORDER4','1000,2000,30001',NULL);
In this case the field GROUPTOCHANGE will be filled by the user like this "2108,8090,8843". Each number represents a PKNUMBER in same table "SAMPLE_DATA".
yes i know! the user can type something wrong.. let's ignore this for now!
The field STATUS will eventually be updated to 'C','L','R' OR NULL. When this happens I Need this logic to be executed:
IF OLD.STATUS <> NEW.STATUS AND GROUPTOCHANGE IS NOT NULL THEN
UPDATE SAMPLE_DATA SP
SET SP.STATUS = :NEW.STATUS
WHERE SP.PKNUMBER IN (:NEW.GROUPTOCHAGE)
AND PS.GROUPTOCHANGE IS NULL;
END IF;
Dispite the bad design is it possible to do?
Thank for any help!!
Here what I've done so far:
create or replace PROCEDURE "AD_LIBERA_FRETES_FILHOS"(
P_CODUSU NUMBER,
P_IDSESSAO VARCHAR2,
P_QTDLINHAS NUMBER,
P_MENSAGEM OUT VARCHAR2)
AS
P_NUNOTA NUMBER(10);
P_CONTROLE VARCHAR(100);
P_PEDIDOS VARCHAR(100);
P_STATUS VARCHAR(100);
BEGIN
-- avoid more than 1 at a time
IF (P_QTDLINHAS > 1) THEN
RAISE_APPLICATION_ERROR(-20000, 'SELECIONE APENAS UM PEDIDO PARA EXECUTAR ESTA AÇÃO.');
END IF;
FOR I IN 1..P_QTDLINHAS LOOP
--extract param from session
P_NUNOTA := ACT_INT_FIELD(P_IDSESSAO, I, 'PKNUMBER');
P_STATUS := ACT_TXT_FIELD(P_IDSESSAO, I, 'STATUS');
--verify typed text should be "84090,89830,83393..."
BEGIN
SELECT REGEXP_REPLACE(CAB.GROUPTOCHANGE, '[0-9-, ]', ''),
CAB.GROUPTOCHANGE
INTO P_CONTROLE,
P_PEDIDOS
FROM SAMPLE_DATA CAB
WHERE CAB.PKNUMBER = P_NUNOTA;
END;
IF (P_CONTROLE IS NOT NULL) THEN
RAISE_APPLICATION_ERROR(-20000, '<B> SOMETHING WRONG !</B>');
ELSE
--perform de update (not working)
BEGIN
UPDATE SAMPLE_DATA C
SET C.STATUS = P_STATUS
WHERE
C.GROUPTOCHANGE IS NULL AND
C.PKNUMBER IN
(WITH DATA AS
(SELECT CAB.GROUPTOCHANGE STR
FROM SAMPLE_DATA CAB
WHERE CAB.PKNUMBER = P_NUNOTA )
SELECT TRIM(REGEXP_SUBSTR(STR, '[^,]+', 1, LEVEL)) STR
FROM DATA CONNECT BY INSTR(STR, ',', 1, LEVEL - 1) > 0);
END;
END IF;
END LOOP;
--mgs to show
P_MENSAGEM := 'DONE!! CHECK -> '||P_PEDIDOS;
END;

Inserting into a table using a procedure only if the record doesn't exist yet

I have a table that i'm trying to populate via a plsql script (runs on plsql developer). The actual DML statement
is contained in a procedure inside a package. The procedure only inserts if the record doesn't exist yet.
It doesn't work. The part that checks for existence returns true after the first iteration of the script loop even if it doesn't actually exist in the table.
If i put the commit outside of the loop, nothing gets inserted at all and the existence checks return true for all iteration even if the table it empty.
When i try to simplify the insert with existence check to be in just one statement without the exception handling, i get the same outcome.
Please tell me what I'm doing wrong here.
CREATE OR REPLACE PACKAGE BODY some_package
IS
PROCEDURE add_to_queue(id IN NUMBER)
IS
pending_record VARCHAR2(1);
BEGIN
-- this part succeeds even if nothing matches the criteria
-- during the loop in the outside script
SELECT 'Y'
INTO pending_record
FROM dual
WHERE EXISTS (SELECT 'x' FROM some_queue smq
WHERE smq.id = id AND smq.status IS NULL);
EXCEPTION
WHEN NO_DATA_FOUND THEN
INSERT INTO some_queue (seqno, id, activity_date)
VALUES (some_sequence.nextval, id, SYSDATE);
WHEN OTHERS THEN
NULL;
END;
END some_package;
CREATE TABLE some_queue
(
seqno VARCHAR2(500) NOT NULL,
id NUMBER NOT NULL,
activity_date DATE NOT NULL,
status VARCHAR2(25),
CONSTRAINT some_queue_pk PRIMARY KEY (seqno)
);
-- script to randomly fill in the table with ids from another table
declare
type ids_coll_tt is table of number index by pls_integer;
ids_coll_table ids_coll_tt;
cursor ids_coll_cur is
select tab.id
from (select *
from ids_source_table
order by dbms_random.value ) tab
where rownum < 10;
begin
open ids_coll_cur;
fetch ids_coll_cur bulk collect into ids_coll_table;
close ids_coll_cur;
for x in 1..ids_coll_table.count
loop
some_package.add_to_queue(ids_coll_table(x));
commit; -- if this is here, the first iteration gets inserted
end loop;
-- commit; -- if the commit is done here, nothing gets inserted
end;
Note: I translated this code to be more generic for posting. Forgive me if there are any typos.
Update: even if i put everything inside the script and not use the package, i'm not able to properly check for existence and I get the same results.
I figured out the solution:
CREATE OR REPLACE PACKAGE BODY some_package
IS
PROCEDURE add_to_queue(p_id IN NUMBER)
IS
pending_record VARCHAR2(1);
BEGIN
-- this part succeeds even if nothing matches the criteria
-- during the loop in the outside script
SELECT 'Y'
INTO pending_record
FROM dual
WHERE EXISTS (SELECT 'x' FROM some_queue smq
WHERE smq.id = p_id AND smq.status IS NULL);
EXCEPTION
WHEN NO_DATA_FOUND THEN
INSERT INTO some_queue (seqno, id, activity_date)
VALUES (some_sequence.nextval, p_id, SYSDATE);
WHEN OTHERS THEN
NULL;
END;
END some_package;
changing the parameter name fixed it. I guess the compiler gets confused if it's the same name as the table field.
Don't name the parameter the same as the column (use a prefix like p_ or in_) and you can do it in a single statement if you use a MERGE statement self-joining on the ROWID pseudo-column:
CREATE OR REPLACE PACKAGE BODY some_package
IS
PROCEDURE add_to_queue(
in_id IN NUMBER
)
IS
BEGIN
MERGE INTO some_queue dst
USING ( SELECT ROWID AS rid
FROM some_queue
WHERE id = in_id
AND status IS NULL ) src
ON ( src.rid = dst.ROWID )
WHEN NOT MATCHED THEN
INSERT (seqno, id, activity_date)
VALUES (some_sequence.nextval, in_id, SYSDATE);
END;
END some_package;

subquery in oracle trigger

I want include condition in my trigger, based on data from a table. As oracle to do allow subquery in trigger, how it can be achived. Please find my code below. Trans_code_master holds the list of valid code, which can be changed.
Thanks in advance for all your help.
CREATE OR REPLACE TRIGGER CUST_TRG
BEFORE INSERT OR UPDATE ON CUST_ALL_TRANS
REFERENCING OLD AS OLD NEW AS NEW
FOR EACH ROW
WHEN (NEW.TRANSACTION_CODE IN(SELECT TRANS_CODE FROM TRANS_CODE_MASTER))
BEGIN
INSERT INTO CUST_DEPO_TRANS
(
CUST_ID
,AC_ID
,TRANSACTION_CODE
)
VALUES(
:NEW.CUST_ID
,:NEW.AC_ID
,:NEW.TRANSACTION_CODE
)
EXCEPTION
WHEN OTHERS THEN
-- Consider logging the error and then re-raise
RAISE;
END;
/
Its not possible in write query in when clause. Try the below way
CREATE OR REPLACE TRIGGER CUST_TRG
BEFORE INSERT OR UPDATE ON CUST_ALL_TRANS
REFERENCING OLD AS OLD NEW AS NEW
FOR EACH ROW
DECLARE
V_CNT NUMBER;
BEGIN
SELECT COUNT(1) INTO V_CNT
FROM TRANS_CODE_MASTER
WHERE TRANS_CODE = NEW.TRANSACTION_CODE;
IF V_CNT > 0 THEN
INSERT INTO CUST_DEPO_TRANS
(
CUST_ID
,AC_ID
,TRANSACTION_CODE
)
VALUES(
:NEW.CUST_ID
,:NEW.AC_ID
,:NEW.TRANSACTION_CODE
)
END IF;
EXCEPTION
WHEN OTHERS THEN
-- Consider logging the error and then re-raise
RAISE;
END;
/

Trigger to disable insert into a table for a specific day in oracle

How can i correctly write a trigger for this task:
create or replace trigger M_t2
after insert on emp
begin
if ( to_char(sysdate,'DY' ) = 'TUE' ) then
dbms_output.put_line('cannot insert into emp on tuesday');
end if;
end;
/
This does not work as i am still able to insert like this:
insert into emp(empno,ename,job,mgr,hiredate,sal,comm,deptno) values('7935','BOLT','ANALYST',7698,sysdate,900,100,10);
dbms_output doesn't stop you doing anything, and you won't even see that message if your client isn't set up to show the output.
To prevent an action you'd need to raise an exception:
if ( to_char(sysdate,'DY' ) = 'TUE' ) then
raise_application_error(-20001, 'cannot insert into emp on tuesday');
end if;
The DY value is NLS-dependent so this could be circumvented by having a session with a different language, so you should take that into account using the optional third parameter to to_char(). It might as well be a before-insert trigger too:
create or replace trigger M_t2
before insert on emp
begin
if ( to_char(sysdate, 'DY', 'NLS_DATE_LANGUAGE=ENGLISH' ) = 'TUE' ) then
raise_application_error(-20001, 'cannot insert into emp on tuesday');
end if;
end;
/
insert into emp ...
ERROR at line 1:
ORA-20001: cannot insert into emp on tuesday
ORA-06512: at "<schema>.N_T2", line 3
ORA-04088: error during execution of trigger '<schema>.M_T2'
Please look at this: http://www.techonthenet.com/oracle/triggers/after_insert.php
I think you must use :new clause and use variable

Resources