Terminate script immediately - plsql

im new with PL_SQL, i want to ask a question. A need to terminate this script when reaches te exit condition, but its failing, it continues. Im not able to terminate the execution of the script when WHENEVER SQLERROR EXIT SQL.SQLCODE is reached. Any suggestions??
WHILE V_COUNT_MENSAJES>0
LOOP
DELETE
FROM TB_ICP_ENVIOMENSAJES
WHERE KEY_MENSAJE = P_KEY_MENSAJE;
WHENEVER SQLERROR EXIT SQL.SQLCODE;
END LOOP;
END;
WHEN others THEN
dbms_output.put_line('Error en la consulta!');
END;
-----REST OF CODE

Try to incorporate the below snippet it will help to terminate the
code whenever you reach any exception. Hope this helps.
DECLARE
p_err_cd PLS_INTEGER;
p_err_msg VARCHAR2(32767 CHAR);
V_COUNT_MENSAJES PLS_INTEGER;
P_KEY_MENSAJE VARCHAR2(1000 CHAR);
BEGIN
WHILE V_COUNT_MENSAJES > 0
LOOP
BEGIN
DELETE FROM TB_ICP_ENVIOMENSAJES WHERE KEY_MENSAJE = P_KEY_MENSAJE;
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20001,SQLERRM,TRUE);
END;
END LOOP;
END;

You don't have to use "WHENEVER SQLERROR EXIT SQL.SQLCODE;" code inside PLSQL block.Whenever error occur control will pass to exception.Please see the below example for the same.
DECLARE
V_COUNT_MENSAJES NUMBER:=10;
ERROR_1 EXCEPTION;
BEGIN
WHILE V_COUNT_MENSAJES>0
LOOP
RAISE ERROR_1;
END LOOP;
EXCEPTION
WHEN others THEN
dbms_output.put_line('Error en la consulta!');
END;
/
Also please note when you’re executing a script, there are many cases in which you want an error to cause SQL*Plus to terminate where we use the following command and not inside plsql block:
SQL> WHENEVER SQLERROR EXIT SQL.SQLCODE

Related

Warning: Trigger created with compilation errors?

I am trying to implement Trigger that raises an User Defined Error Message and does not allow the update and Insert operation in the database.I am new to pl/sql i refereed some code from the internet and try to implement.My code is running is fine as i can not update/insert into database but still i am unable to get my user defined message and also i am getting this warning.
Warning: Trigger created with compilation errors ?
This is the table :
Name Null? Type
----------------------------------------- -------- ----------------------------
ID NUMBER(5)
NAME VARCHAR2(20)
SALARY NUMBER(10)
DEPOT_ADDRESS VARCHAR2(15)
here is my code :
create or replace trigger cleaner_before_update_insert
for update or insert on cleaner
compound trigger
count binary_integer;
before statement is
begin
count:=0;
end before statement;
after each row is
begin
count :=count +1;
end after each row;
after statement is
begin
if count > 0 then
raise_application_error( -20001,'Update/insert operation can not be completed ');
end if;
end after statement;
end cleaner_before_update;
/
can anyone help me figure out what is the problem here and way to fix it.
thanks in advance.
Even after compilation of the code it is giving me this error.
ORA-06512: at "SYSTEM.CLEANER_BEFORE_UPDATE_INSERT", line 18
ORA-04088: error during execution of trigger
'SYSTEM.CLEANER_BEFORE_UPDATE_INSERT'
There are couple of problems with your TRIGGER block.
The name of the Trigger cleaner_before_update_insert does not match with the cleaner_before_update after the final end statement.
COUNT is an SQL keyword which is not allowed to be used as a variable in PL/SQL.You would receive the error - Error(10,11): PLS-00204: function or pseudo-column 'COUNT' may be used inside a SQL statement only
So, here is the modified code.
CREATE OR REPLACE TRIGGER cleaner_before_update_insert FOR UPDATE OR
INSERT ON cleaner compound TRIGGER
v_count binary_integer;
before STATEMENT
IS
BEGIN
v_count:=0;
END before STATEMENT;
AFTER EACH row
IS
BEGIN
v_count :=v_count +1;
END AFTER EACH row;
AFTER STATEMENT
IS
BEGIN
IF v_count > 0 THEN
raise_application_error( -20001,'Update/insert operation can not be completed ');
END IF;
END AFTER STATEMENT;
END cleaner_before_update_insert;
/

How to remove extra information form error message after executing a package body?

I want to remove extra information form error message after executing a package body.
Example:
If I run a anonymous block then the error message come like -
ERROR at line 1:
ORA-20010: Object ID PE556092 is not Produced by the Template TP000036
ORA-06512: at "EXAMPLE.TESTPROC", line 5
ORA-06512: at line 1
But i want to show only the below error message:
ORA-20010: Object ID PE556092 is not Produced by the Template TP000036
You can use the below to get the Error Code and Message respectively:
dbms_output.put_line('Error code: '||sqlcode);
dbms_output.put_line('Error msg: '||sqlerrm);
Read more about SQLERRM.
You can do that by defining an exception and mapping it to the SQLCODE of the raised error. Then you handle the logic in the exception handler.
Example, here I map to error -20010 (the one you are using):
declare
err_obj_not_produced exception;
pragma exception_init (err_obj_not_produced, -20010);
l_object varchar2 (50);
l_template varchar2 (50);
begin
run_your_code ('With values');
exception
when err_obj_not_produced then
return 'Object ID ' || l_object || ' is not Produced by the Template ' || l_template;
end;
Another one way to hide information about previous errors. Suppose you have two procedures:
create or replace procedure proc1 is
begin
raise no_data_found;
end;
/
create or replace procedure proc2 is
begin
proc1;
exception
when no_data_found then
raise_application_error(-20010, 'There is no spoon, Neo.');
end;
/
when you run a block in SQL*Plus, you get:
SQL> begin
proc2;
end;
/
begin
*
ERROR at line 1:
ORA-20010: There is no spoon, Neo.
ORA-06512: at "DEV.PROC2", line 6
ORA-06512: at line 2
You can change proc2 like this:
create or replace procedure proc2 is
begin
proc1;
end;
/
And call it like this with desired result:
SQL> begin
begin
proc2;
exception
when no_data_found then
raise_application_error(-20010, 'There is no spoon, Neo.', false);
end;
exception
when others then
dbms_output.put_line(SQLERRM);
end;
/
ORA-20010: There is no spoon, Neo.
PL/SQL procedure successfully completed.
This example shows how third parameter of raise_application_error procedure works. If you pass false, procedure deletes information about previously raised exceptions from error stack. It also will be impossible to get this information using dbms_utility.format_error_backtrace.
As Jon Heller noticed in comments, hiding information about exceptions is a bad practice (and using when others then without raise too, as Jeffry Kemp noticed), so, I hope you know what you are doing. This way or another, it is a shooting yourself in the foot.

return keyword wont work... how to stop running of program?

I had two buttons that do different job but there is check data entry in common
so I made a program unit for that check then I call it from these two buttons
but my problem is that when there is an error within the check I got the message for user and all things I made but the its get back to the code within button and continue progressing I set a return keywords at end of each condition to sop the code from running but its not working please whats the problem ? how I can stop my code until the error check passed ?!!
example of check data entry program unit
PROCEDURE CHECK_ENTRY IS
BEGIN
IF :block1.text_item1 IS NULL THEN
SET_ITEM_INSTANCE_PROPERTY('block1.text_item1',CURRENT_RECORD,VISUAL_ATTRIBUTE,'ERROR_ATR');
SHOW_MESSAGE('example msg .');
RETURN;
ELSIF :block2.text_item2 IS NULL THEN
SET_ITEM_INSTANCE_PROPERTY('block2.text_item2',CURRENT_RECORD,VISUAL_ATTRIBUTE,'ERROR_ATR');
SHOW_MESSAGE('example msg2 .');
RETURN;
END IF ;
END;
example of code within on_button_press trigger
PROCEDURE procedure_name IS
BEGIN
IF FORM_SUCCESS THEN
DISPLAY_ERROR;
:block1.text_item1:= :block2.text_item2;
:block2.text_item2:=:block1.text_item1;
**CHECK_ENTRY;** /* here is the calling of previous program unit that check the data entry then get back to here in case there is no error and continue commit the data and disabling text item so user wont be able to modify the data */
COMMIT_FORM;
program_unit('ORDER_DONE');
ELSE
ROLLBACK;
DISPLAY_ERROR;
SHOW_MESSAGE('please connect administrator.');
END IF;
END;
Your code should be like this:
PROCEDURE CHECK_ENTRY IS
BEGIN
IF :block1.text_item1 IS NULL THEN
SET_ITEM_INSTANCE_PROPERTY('block1.text_item1',CURRENT_RECORD,VISUAL_ATTRIBUTE,'ERROR_ATR');
SHOW_MESSAGE('example msg .');
raise form_trigger_failure;
ELSIF :block2.text_item2 IS NULL THEN
SET_ITEM_INSTANCE_PROPERTY('block2.text_item2',CURRENT_RECORD,VISUAL_ATTRIBUTE,'ERROR_ATR');
SHOW_MESSAGE('example msg2 .');
raise form_trigger_failure;
END IF ;
END;
PROCEDURE procedure_name IS
BEGIN
:block1.text_item1:= :block2.text_item2;
:block2.text_item2:=:block1.text_item1;
CHECK_ENTRY;
IF FORM_SUCCESS THEN
DISPLAY_ERROR;
COMMIT_FORM;
program_unit('ORDER_DONE');
ELSE
ROLLBACK;
DISPLAY_ERROR;
SHOW_MESSAGE('please connect administrator.');
END IF;
END;

Exception handling - plsql

I have a problem. Am doing an insert function
case f1.RFD_CATEGORY_CODE when'O1' then 'C1GBC'
when 'O2' then 'C2GBC' else null end
the field is mandatory and thus instead of null i need to show an error message if the code is not taking C1GBC or C2GBC. and if the code is taking C1GBC or C2GBC then show successful as message.
i have create an exception below but am getting error
create or replace procedure CTP_CODE as
declare
--RFD_CAT_ERR varchar2;
RFD_CAT_ERR EXCEPTION;
begin
if RFD_CATEGORY_CODE is '01' then RFD_CATEGORY_CODE is 'C1GBC';
DBMS_OUTPUT.PUT_LINE ('No1. Successful Operation');
else
if RFD_CATEGORY_CODE is '02' then RFD_CATEGORY_CODE is 'C2GBC';
DBMS_OUTPUT.PUT_LINE ('No2. Successful Operation');
end if;
raise RFD_CAT_ERR;
end if;
EXCEPTION
when RFD_CAT_ERR then
DBMS_OUTPUT.PUT_LINE ('Error message!');
end;
/
Wrong Code also wrong syntax..
Whenever your code goes in else condition it will throw error as after executing 2nd if it will go to "raise RFD_CAT_ERR;" part and raise an exception.
so u should handle error in elsif after 2nd If condition.
You say the field is mandatory. If by that you mean the field in the table is constrained as not null then you don't have to worry about raising an exception. The insert statement will do that for you. All you have to do is catch it in your exception handler and use RAISE_APPLICATION_ERROR to return a meaningful message.
Do that rather than try to print an error message. If your procedure is called by a batch process, there will be no one there to see it.
This works for me :D
CASE f1.RFD_CATEGORY_CODE
WHEN 'O1' THEN 'C1GBC'
WHEN 'O2' THEN 'C2GBC'
ELSE 'error'
Then raise any errors for exception

Catching a constraint violation in psql

I am using sql developer, and have added a constraint to one of my tables.
constraint valid_gender check(gender in ('M','F','I','T'))
When I try to add an entry with say 'x' for gender using a plsql procedure, it fails with constraint violation (as it should).
I want to add a "Catch" to the plsql procedure so that if valid_gender is voilated I can raise_application_error specific to it. Is this possible?
Oracle will raise an exception that says:
ORA-02290: check constraint (yourschema.valid_gender) violated
You can catch that in an exception handler and raise your own exception instead using raise_application_error in a couple of ways.
1) You can specifically trap the ORA-02290 exception like this:
declare
e_check_violated exception
pragma exception_init (e_check_violated, -2290);
begin
insert ...
exception
when e_check_violated then
if sqlerrm like '%(yourschema.valid_gender)%' then
raise_application_error(-20001,'Invalid gender');
else
raise;
end if;
end;
2) You can trap all exceptions and inspect them:
begin
insert ...
exception
when others then
if sqlerrm like 'ORA-02290:%(yourschema.valid_gender)%' then
raise_application_error(-20001,'Invalid gender');
else
raise;
end if;
end;
In a large application it is quite common to have an exception handling procedure to generalise this and look up the constraint-specific message in a table.
use anonym block in your code...
BEGIN
INSERT or update...
EXCEPTION
WHEN dup_val_on_index THEN
RISE...
END;
You could just test it first:
if gender_value not in ('M','F','I','T') then
raise_application_error...
end if;

Resources