PL/SQL add select query inside exception? - oracle

Currently I have this PL/SQL function inside a package:
function populate_booking_map( pi_event_id in varchar2
,pi_status in number
,pi_booking_id in varchar2
)
return integer as
begin
insert into booking_map (
booking_id
,event_id
,currentstatus
)
values(
pi_booking_id
,pi_event_id
,pi_status
);
return c_success;
exception
when OTHERS then
log_error_msg( pi_app_name => c_application_name
,pi_error_msg => 'Exception in populate booking map.'
,pi_error_details => sqlerrm
);
return c_failure;
end populate_booking_map;
I want to add catching another exception - DUP_VAL_ON_INDEX - before OTHERS to handle exception when the attempt-to-insert record already exists in table.
Inside the handler, I want to check the existing values are same as the provided, i.e.: pi_booking_id=booking_id and pi_event_id=event_id and pi_status=currentstatus .
If they are completely the same, return c_success;.
If they are not the same, return c_failure;
How should I do that? Can I a SELECT query inside the exception handling? I have never seen anybody doing that.

You can have a whole new PL/SQL block in exception handling section (i.e. not just the SELECT statement).
For example:
CREATE FUNCTION populate_booking_map (parameters here)
RETURN INTEGER
AS
l_dummy VARCHAR2 (1);
BEGIN
NULL;
EXCEPTION
WHEN DUP_VAL_ON_INDEX
THEN
SELECT dummy INTO l_dummy FROM DUAL; --> but what if this returns an error?
RETURN c_success;
WHEN OTHERS
THEN
RETURN c_failure;
END;
CREATE FUNCTION populate_booking_map (parameters here)
RETURN INTEGER
AS
l_dummy VARCHAR2 (1);
BEGIN
NULL;
EXCEPTION
WHEN DUP_VAL_ON_INDEX
THEN
BEGIN --> new PL/SQL block
SELECT dummy INTO l_dummy FROM DUAL;
EXCEPTION --> with its own EXCEPTION
WHEN NO_DATA_FOUND
THEN
RETURN c_failure;
END; --> end of that block
RETURN c_success;
WHEN OTHERS
THEN
RETURN c_failure;
END;
However: if you don't want to take care about handling DUP_VAL_ON_INDEX, write INSERT so that it does nothing if values (passed as parameters) already exist in the table:
CREATE FUNCTION populate_booking_map (parameters here)
RETURN INTEGER
IS
BEGIN
INSERT INTO booking_map (booking_id, event_id, currentstatus)
SELECT pi_booking_id, pi_event_id, pi_status
FROM DUAL
WHERE NOT EXISTS --> this will prevent duplicates
(SELECT NULL --> to be inserted
FROM booking_map
WHERE booking_id = pi_booking_id
AND event_id = pi_event_id
AND status = pi_status);
EXCEPTION
WHEN OTHERS
THEN
RETURN c_failure;
END;

Related

How to skip insert to an Oracle table in procedure where specified columns going to insert are null

I'm inserting lots of rows into a table and some of the columns are blank for some of the rows.
How can I skip insert if some important fields are blank?
for example there is a table 'people' and my important fields are name,cityName and age.
1 INSERT INTO people VALUES('customerid1','name', 'cityName', 50, 'anotherValue')
2 INSERT INTO people VALUES('customerid2','', '', '' , 'anotherValue')
3 INSERT INTO people VALUES('customerid3','name', 'cityName', 20, 'anotherValue')
4 INSERT INTO people VALUES('customerid4','name', 'cityName', 19, 'anotherValue')
here 2nd row name,cityName and age are blank.
if those three fields are blank then dont insert that row.this is just an example i have more fields to check so need to avoid 'if condition' to check blank or not.
another example
FUNCTION TEST_FUN (increment_i in VARCHAR2, increment_j IN VARCHAR2,mod_id IN VARCHAR2 )
RETURN numeric IS
j_val VARCHAR2(100);
i_val VARCHAR2(100);
BEGIN
i_val := increment_i;
j_val := increment_j;
IF mod_id != 'loop' THEN
j_val := i_val;
END IF;
INSERT
INTO TEST.testpartytable
(
reffer_id,
customer_id,
customer_joint,
fullname,
nature,
counter_bus,
country,
status
)
VALUES
(
REFFER_ID_AR,
CUSTOMER_ID_ARR(i_val),
CUSTOMER_JOINT,
LEGALNAME_KBC_ARR(i_val),
NATURERE_KBC_ARR(j_val),
COUNTERBUSACT_KBC_ARR(j_val),
COUNTRY_KBC_ARR(j_val),
STATUS
);
return i_val;
END TEST_FUN ;
skip insert if 'fullname,nature,counter_bus,country' fields are blank .Datas coming from colletion.
Any help is appreciated.
Thanks!
well you can check first if the values are null or not null:
declare CHECK_VAL varchar2(100);
BEGIN
select CUSTOMER_ID_ARR(i_val) into CHECK_VAL from dual;
if(i_val is not null) then
insert...
end if;
END;
You can alternavely make the column to not null, and raise exeception when you get error for a value not null.
You can apply a NOT NULL constraint to your important columns so that when any of them assigned with a NULL value an error will be raised, specifically ORA -1400 (cannot insert null) exception. You can then catch this exception in your program and just do nothing when this exception is raised.
Sample below,
CREATE TABLE TEST_TABLE
(col1 NUMBER NOT NULL, col2 NUMBER NOT NULL);
DECLARE
CANNOT_INSERT_NULL EXCEPTION;
PRAGMA EXCEPTION_INIT(cannot_insert_null, -1400);
num NUMBER;
BEGIN
FOR i IN 1..10 LOOP
num := 2;
IF i BETWEEN 3 AND 5 THEN
num := NULL; //since this is null, insert statement below will error and handled and won't be inserted
END IF;
BEGIN
INSERt INTO test_table
(col1,col2)
VALUES
(i, num);
EXCEPTION
WHEN CANNOT_INSERT_NULL THEN
NULL;
END;
END LOOP;
END;
/
SELECT *
FROM test_table;

Second statement in begin block is not executed

I have written this stored procedure in Oracle:
CREATE OR REPLACE FUNCTION GET_SOLVER_ID(username_in IN VARCHAR2)
RETURN NUMBER
IS
solver_id number(19);
system_user_id number(19);
BEGIN
SELECT id
INTO solver_id
FROM usr_solver
WHERE username = username_in;
select ID into system_user_id from USR_USER where USER_TYPE = 'X';
solver_id := nvl(solver_id, system_user_id);
RETURN(solver_id);
END;
When I call the function with username that doesn't exist in table usr_solver I get null for the result. I expect to get system_user_id instead.
It seems like the other select statement and nvl function in begin block didn't execute.
Could you help, I can't see the reason why...
Thanks,
mismas
This should do what you want
CREATE OR REPLACE FUNCTION GET_SOLVER_ID(
username_in IN VARCHAR2)
RETURN NUMBER
IS
some_id NUMBER(19);
BEGIN
BEGIN
SELECT id
INTO some_id
FROM usr_solver
WHERE username = username_in;
EXCEPTION
WHEN NO_DATA_FOUND THEN
SELECT ID
INTO some_id
FROM USR_USER
WHERE USER_TYPE = 'X';
END;
RETURN(some_id);
END;

Catch ORA exceptions in PL/SQL

I wrote a package to add records in a country table that has a reference key pointing to a "regions" table using region_id.So,if I try to add a "region_id" in my countries table and if that value does not exist in my regions table,I should throw the exception and catch.
My package code is:
CREATE PACKAGE BODY cus7 AS
v_error_code NUMBER;
region_exists pls_integer;
procedure addi6 (c_cntry_id in out countries.country_id%type,
c_cntr_name in countries.country_name%type,
c_rgn_id in countries.region_id%type)
is
begin
begin
select 1 into region_exists
from regions r
where r.region_id = c_rgn_id;
exception
when no_data_found then
region_exists := 0;
DBMS_OUTPUT.PUT_LINE('Region not present');
end;
if region_exists = 1 then
insert into countries(country_id, country_name,region_id)
values (c_cntry_id, c_cntr_name, c_rgn_id);
DBMS_OUTPUT.PUT_LINE('Inserted');
END IF;
EXCEPTION
WHEN dup_val_on_index
THEN
c_cntry_id := null;
DBMS_OUTPUT.PUT_LINE('Already present');
end addi6;
END cus7;
/
Now,in my procedure,everything is working fine,except when I do an add like this:
DECLARE
outputValue CHAR(2) := 'KO';
begin
cus7.addi6(outputValue,'KOREA',5);
end;
/
apart from getting my own message which is "Region not found",I am also getting ORA-01403-No data found.
My question is if there is a way to catch this ORA exception or avoid display?
Tx in advance
Yes, all you have to do is add WHEN OTHERS to your exception block in order to catch all of the other possible ORA exceptions.
EXCEPTION
WHEN dup_val_on_index
THEN
c_cntry_id := null;
DBMS_OUTPUT.PUT_LINE('Already present')
WHEN OTHERS
THEN
DBMS_OUTPUT.PUT_LINE('Another Error');
-- other logic here
Here is an extensive documentation regarding error handling in PL/SQL.

DBMS Pl/SQL -- What would be output ... please explain?

SQL> DESC hostel;
Name Null? Type
------------------------------------ -------- -------------------
HOSTELID NOT NULL VARCHAR2(4)
ROOMSAVAILABLE NUMBER(3)
HOSTELTYPE VARCHAR2(1)
HOSTELFEE NUMBER(6)
SQL> SELECT * FROM hostel;
HOST ROOMSAVAILABLE H HOSTELFEE
------- ---------------------- ---- ---------------------
H1 2 M 2000
H2 3 F 3000
Above is shown a table hostel and values in it.
What would be the output of following pl/sql program?
please explain in detail.
CREATE OR REPLACE PROCEDURE sp_validatehostelid
(p_hostelid IN hostel.hostelid%TYPE,
p_hostelfee OUT hostel.hostelfee%TYPE
)
IS
v_count NUMBER;
v_hostelfee hostel.hostelfee%TYPE;
BEGIN
SELECT COUNT(*) INTO v_count FROM hostel WHERE hostelid=p_hostelid;
IF v_count=0 THEN
RAISE_APPLICATION_ERROR(-20000,'Invalid Hostel id');
ELSE
SELECT hostelfee INTO v_hostelfee FROM hostel WHERE hostelid=p_hostelid;
DBMS_OUTPUT.PUT_LINE('Hostel Fee:'||v_hostelfee);
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Other Errors in Procedure');
END sp_validatehostelid;
Procedure created.
DECLARE
g_hostelfee hostel.hostelfee%TYPE;
BEGIN
sp_validatehostelid('H5',g_hostelfee);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Other Errors in Block');
END;
"What will be the output? given that there is no row having hostelid =
'H5'"
Assuming you run this in a client with serveroutput enabled the output will be
Other Errors in Procedure
PL/SQL procedure successfully completed.
SQL>
Why?
The first select statement is a count, which cannot hurl a NO_DATA_FOUND exception.
The next line raises a user-defined exception, -20000.
This passes control to the exception handler block. -20000 is not NO_DATA_FOUND so the WHEN OTHERS clause is executed, which displays the message above.
The exception handler does not raise an exception itself, which is very bad practice. So the flow returns to the calling block.
Because no exception was found the calling block thinks the called procedure executed successfully, and so processing terminate cleanly. That's why it is bad practice not re-raise exceptions.
Note that if you run this without enabling serveroutput first the output will be:
PL/SQL procedure successfully completed.
SQL>
CREATE OR REPLACE PROCEDURE sp_validatehostelid
(
p_hostelid IN hostel.hostelid%TYPE,
p_hostelfee OUT hostel.hostelfee%TYPE
)
IS
v_count NUMBER;
v_hostelfee hostel.hostelfee%TYPE;
BEGIN
/* Count rows in 'hostel' table for given ID */
SELECT COUNT(*) INTO v_count FROM hostel WHERE hostelid=p_hostelid;
/* If there is noting in the table */
IF v_count=0 THEN
/* raise exception */
RAISE_APPLICATION_ERROR(-20000,'Invalid Hostel id');
ELSE
/* select fee from the 'hostel' table */
SELECT hostelfee INTO v_hostelfee FROM hostel WHERE hostelid=p_hostelid;
/* print the fee */
DBMS_OUTPUT.PUT_LINE('Hostel Fee:'||v_hostelfee);
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Other Errors in Procedure');
END sp_validatehostelid;
DECLARE
g_hostelfee hostel.hostelfee%TYPE;
BEGIN
sp_validatehostelid('H5',g_hostelfee);
/*
**Here something should be done with 'g_hostelfee' variable
*/
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Other Errors in Block');
END;
If there is a row with hostelid = 'H5' collect the fee for given ID, print it and pass it out.
NOTE: It will work only for one row per ID. If there is more than one. TO_MANY_VALUES exception will be raised.

Function does not return what I am expecting

In my function below I am trying to understand why it only returns BLAH if I pass in 01356666 and then a null value for anything else passed in. My expectation was that it would return BLAH regardless of what was passed in since I reset the str_mgrin_out after I do the SELECT INTO. I have been testing this in Oracle 10g.
CREATE OR REPLACE FUNCTION GET_MANAGERGIN2 (str_empgin_in IN varchar2)
RETURN varchar2
AS
str_mgrgin_out varchar2(10);
BEGIN
SELECT 'FOO' INTO str_mgrgin_out FROM dual WHERE str_empgin_in = '01356666';
str_mgrgin_out := 'BLAH';
RETURN str_mgrgin_out;
END GET_MANAGERGIN2;
/
-- Returns null but expecting BLAH
SELECT GET_MANAGERGIN2('00356666') FROM dual;
-- Returns BLAH
SELECT GET_MANAGERGIN2('01356666') FROM dual;
I'd assume that this is because if the SELECT INTO doesn't return exactly one value into str_mgrgin_out it'll throw an exception, so the str_mgrgin_out := 'BLAH'; line never gets executed.
I think the error would be ORA-01403.
Try adding an exception handler at the end as:
Exception
When Others Then
str_mgrgin_out := 'BLAH';
You might have to surround it with a BEGIN...END as well so it would be:
CREATE OR REPLACE FUNCTION GET_MANAGERGIN2 (str_empgin_in IN varchar2)
RETURN varchar2
AS
str_mgrgin_out varchar2(10);
BEGIN
BEGIN
SELECT 'FOO' INTO str_mgrgin_out FROM dual WHERE str_empgin_in = '01356666';
str_mgrgin_out := 'BLAH';
Exception
When Others THen
str_mgrgin_out := 'BLAH';
END;
RETURN str_mgrgin_out;
END GET_MANAGERGIN2;
The select matches zero rows, with raises a NO_DATA_FOUND PL/SQL exception.
However NO_DATA_FOUND isn't recognized as an SQL error, merely the end of the result set.
Therefore when used in a SELECT, a null value is returned.

Resources