wrirting rollback after handling exception - plsql

I have a scenario in which, i am writing the exception like this
when others then
rollback;
p_status := 'ERROR'; -- MODIFIED
p_status_dtl := sqlcode||' - '||substr(sqlerrm,1,100);
end;
if i write like this
exception
when others then
p_status := 'ERROR'; -- MODIFIED
p_status_dtl := sqlcode||' - '||substr(sqlerrm,1,100);
rollback;
end;
does it make a difference if yes what is the difference.

The examples are identical if the variable assignments don't throw (i.e. both p_status and p_status_dtl are correct data type with enough storage).
If the variable assignment throws then in the second example rollback is not executed.
In the example below only checkpoint 1 will be printed:
declare
v_foo varchar2(2) := 'AB';
v_bar number;
begin
-- raises ORA-01476: divisor is equal to zero
v_bar := 1/0;
exception
when others then
dbms_output.put_line('checkpoint 1');
-- raises ORA-06502: PL/SQL: numeric or value error: character string buffer too small
v_foo := 'TOO LONG';
dbms_output.put_line('checkpoint 2');
end;
/

Related

Loop cursor of ALL_MVIEWS generate PL/SQL Error ORA-06502

i wrote a procedure that deals with writing the contents of the QUERY column of ALL_MVIEWS to a file:
DECLARE
v_out_dir_name VARCHAR2(30) := 'DIR_TEST';
v_out_dir_path VARCHAR2(60);
v_count_object_elab NUMBER := 0;
CURSOR c_mviews IS
SELECT
LOWER(MVIEW_NAME) || '.sql' AS FILE_NAME
, QUERY AS SCRIPT
FROM ALL_MVIEWS
;
v_file UTL_FILE.file_type;
BEGIN
FOR r_mview IN c_mviews LOOP
v_file := UTL_FILE.fopen (v_out_dir_name, r_mview.FILE_NAME, 'w');
UTL_FILE.putf (v_file, r_mview.SCRIPT);
UTL_FILE.fclose (v_file);
v_count_object_elab := v_count_object_elab + 1;
END LOOP;
IF v_count_object_elab = 0
THEN
DBMS_OUTPUT.PUT_LINE('NESSUN FILE ELABORATO');
END IF;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('ERRORE = ' || SQLERRM);
IF UTL_FILE.IS_OPEN (v_file) THEN
UTL_FILE.FCLOSE (v_file);
END IF;
RAISE;
END;
/
But the "FOR r_mview IN c_mviews LOOP" statement generates the following error:
Report error -
ORA-06502: PL/SQL: errore di numero o valore
ORA-06512: a line 35
ORA-06512: a line 16
ORA-06512: a line 16
06502. 00000 - "PL/SQL: numeric or value error%s"
*Cause: An arithmetic, numeric, string, conversion, or constraint error
occurred. For example, this error occurs if an attempt is made to
assign the value NULL to a variable declared NOT NULL, or if an
attempt is made to assign an integer larger than 99 to a variable
declared NUMBER(2).
*Action: Change the data, how it is manipulated, or how it is declared so
that values do not violate constraints.
The error is thrown on a materialized view that has QUERY_LEN = 39000.
How can I solve the problem?
Many thanks in advance.
I don't think you are using UTL_FILE correctly. The putf() procedure is for writing and formatting (using printf() style). Try using just put() instead. I did run your script on my database and it executed fine with putf() and put(), but put() is more appropriate.
Also, beware that QUERY is a LONG. If you have a query over 32k I think you will get the error you are seeing. There are some great write-ups online about how awful LONGs are to work with.
I think easiest thing to do is convert the LONG to CLOB with a CREATE TABLE, read that table and then drop it. Here is a routine to do just that. It will create a copy of all_mviews and convert the query to a CLOB and write that in chunks to a file per view.
DECLARE
v_out_dir_name VARCHAR2(30) := 'MVIEW';
v_count_object_elab NUMBER := 0;
v_cursor SYS_REFCURSOR;
v_file utl_file.file_type;
v_start INTEGER;
v_buffer VARCHAR2(1024);
v_file_name VARCHAR2(1024);
v_query CLOB;
table_or_view_does_not_exist EXCEPTION;
PRAGMA EXCEPTION_INIT(table_or_view_does_not_exist,
-00942);
BEGIN
BEGIN
EXECUTE IMMEDIATE 'DROP TABLE all_mviews_clob';
EXCEPTION
WHEN table_or_view_does_not_exist THEN
NULL;
END;
EXECUTE IMMEDIATE 'CREATE TABLE all_mviews_clob AS SELECT mview_name, to_lob(query) AS query FROM all_mviews';
OPEN v_cursor FOR q'[SELECT lower(mview_name) || '.sql' AS file_name,
query AS script
FROM all_mviews_clob]';
LOOP
FETCH v_cursor
INTO v_file_name,
v_query;
EXIT WHEN v_cursor%NOTFOUND;
v_file := utl_file.fopen(location => v_out_dir_name,
filename => v_file_name,
open_mode => 'w');
v_start := 1;
FOR i IN 1 .. ceil(dbms_lob.getlength(lob_loc => v_query) / 1024)
LOOP
v_buffer := dbms_lob.substr(lob_loc => v_query,
amount => 1024,
offset => v_start);
IF v_buffer IS NOT NULL THEN
utl_file.put(file => v_file,
buffer => v_buffer);
utl_file.fflush(file => v_file);
END IF;
v_start := v_start + 1024;
END LOOP;
utl_file.fclose(v_file);
v_count_object_elab := v_count_object_elab + 1;
END LOOP;
IF v_count_object_elab = 0 THEN
dbms_output.put_line('no mviews');
END IF;
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line('eror = ' || SQLERRM);
IF utl_file.is_open(v_file) THEN
utl_file.fclose(v_file);
END IF;
RAISE;
END;
/

"Identifier must be declared" when using a function

I have the following function in a package body:
FUNCTION valida_salario(p_salary IN employees.salary%TYPE,
p_department_id IN employees.department_id%TYPE)
RETURN BOOLEAN IS
v_prom_depto NUMBER;
v_var_depto NUMBER;
BEGIN
SELECT AVG(salary), VARIANCE(SALARY) INTO v_prom_depto, v_var_depto
FROM employees
WHERE department_id=p_department_id;
-- GROUP BY department_id;
IF p_salary < v_prom_depto + 3* sqrt(v_var_depto) THEN
RETURN TRUE;
ELSE
RETURN FALSE;
END IF;
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line(SQLERRM);
END valida_salario;
And when I am using the function in a stored procedure (the procedure is in the body too), it show errors:
PROCEDURE crea_empleado(p_last_name IN employees.last_name%TYPE,
p_first_name IN employees.first_name%TYPE,
p_email IN employees.email%TYPE,
p_hire_date IN employees.hire_date%TYPE,
p_job_id IN employees.job_id%TYPE,
p_department_id IN employees.department_id%TYPE
DEFAULT 80,
p_resultado OUT NUMBER) AS
e_salario_no_valido EXCEPTION;
BEGIN
IF valida_salario(p_salary, p_department_id) THEN // Here is located the errors
NULL;
ELSE
RAISE e_salario_no_valido;
END IF;
INSERT INTO employees(employee_id, last_name, first_name, email, hire_date, job_id)
VALUES (emp_seq.NEXTVAL, p_last_name, p_first_name, p_email, p_hire_date, p_job_id);
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
p_resultado := 2;
dbms_output.put_line('Datos duplicados');
WHEN e_salario_no_valido THEN
p_resultado := 1;
WHEN OTHERS THEN
dbms_output.put_line(SQLERRM);
ROLLBACK;
END crea_empleado;
The following errors are :
PL/SQL: Statement ignored and
PLS-00201: the identifier 'P_SALARY' must be declared
But I can't see the error in my function, so I don't understand why it shows these errors.
Thanks in advance for your help
You need to pass p_salary into your crea_empleado() procedure. p_department_id is there but no p_salary.

how to handle exception in if condition pl/sql

ineed your help when i wrote if else condition in plsql to check whether it is even or odd number using if conditopn . if i enter character then it will raise an error.how to handle it using exceptions to understand client. help me
declare
num number:=&number;
begin
if num=1 then
dbms_output.put_line('composite');
else if mod(num,2)=0 then
dbms_output.put_line('even');
else if mod(num,2)=1 then
dbms_output.put_line('odd');
else
dbms_output.put_line('enter proper data');
end if;
end if;
end if;
end;
/
Please see the following working example:
declare
-- user input is always a string so treat it as a string
v_input varchar2(32767) := '&number';
num number;
begin
-- convert input to a number. to_number() throws if input string can't be
-- converted to a number. see to_number() documentation for details.
-- the conversion has to happen inside begin-end block because the exception
-- handler of the block won't catch exceptions raised in declare block.
num:=to_number(v_input);
-- do your math
if num=1 then
dbms_output.put_line('composite');
elsif mod(num,2)=0 then
dbms_output.put_line('even');
elsif mod(num,2)=1 then
dbms_output.put_line('odd');
else
dbms_output.put_line('enter proper data');
end if;
exception
-- the exception raised by to_number()
when value_error then
dbms_output.put_line('not a valid number: ' || v_input);
-- all other exceptions are still raised to the caller.
end;
/
Example results:
SQL> #so
Enter value for number: 1
old 2: v_input varchar2(32767) := '&number';
new 2: v_input varchar2(32767) := '1';
composite
PL/SQL procedure successfully completed.
SQL> #so
Enter value for number: 2
old 2: v_input varchar2(32767) := '&number';
new 2: v_input varchar2(32767) := '2';
even
PL/SQL procedure successfully completed.
SQL> #so
Enter value for number: 3
old 2: v_input varchar2(32767) := '&number';
new 2: v_input varchar2(32767) := '3';
odd
PL/SQL procedure successfully completed.
SQL> #so
Enter value for number: foo
old 2: v_input varchar2(32767) := '&number';
new 2: v_input varchar2(32767) := 'foo';
not a valid number: foo
PL/SQL procedure successfully completed.
SQL>

wrong number or types of arguments in call to my procedure

hi I wrote this code to create a procedure to return a Boolean value based on the if conditions but when I execute it I got this error:
ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'DDPAY_SP'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
here is my procedure
create or replace procedure DDPAY_SP (
donor_id dd_donor.iddonor%type,
pldgstatus out dd_pledge.idstatus%type,
monthplan out dd_pledge.paymonths%type,
ret out boolean)
IS
begin
select idstatus, paymonths into
pldgstatus, monthplan from dd_pledge
where iddonor = donor_id ;
if (pldgstatus = 10 AND monthplan >0)
then ret:= true;
else
ret:= false;
end if;
end;
and this how I execute it
EXECUTE DDPAY_SP (308);
I didn't put much talk I hope it's clear enough for you
I read online it recommends me to check the naming also the data type which I did but nothing change
any ideas
If you don't need the second and third arguments you could declare those as variables in the procedure instead of arguments, as follows:
CREATE OR REPLACE PROCEDURE DDPAY_SP(DONOR_ID IN DD_DONOR.IDDONOR%TYPE,
RET OUT BOOLEAN)
IS
nPayment_count NUMBER;
BEGIN
SELECT COUNT(*)
INTO nPayment_count
FROM DD_PLEDGE p
WHERE p.IDDONOR = DONOR_ID AND
p.IDSTATUS = 10 AND
p.PAYMONTHS > 0;
IF nPayment_count > 0 THEN
RET := TRUE;
END IF;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('DD_PAY - exception: ' || SQLCODE || ' : ' || SQLERRM);
RAISE;
END DDPAY_SP;
I've included an example of an EXCEPTION handler at the end of DD_PAY. It's always a good idea to include at least this minimal handler so that in the event an exception occurs you'll get some indication of where the problem lies.
Because this procedure returns a BOOLEAN value, and BOOLEANs cannot (to the best of my knowledge) be used from SQL*Plus, you'll have to invoke it from a PL/SQL block, as follows:
DECLARE
bRetval BOOLEAN;
BEGIN
DD_PAY(308, bRetval);
DBMS_OUTPUT.PUT_LINE('Returned value is ' ||
CASE bRetval
WHEN TRUE THEN 'TRUE'
ELSE 'FALSE'
END);
END;
Give that a try.
EDIT: rewrote procedure based on further information from later comments.
Share and enjoy.

Check a record IS NOT NULL in plsql

I have a function which would return a record with type my_table%ROWTYPE, and in the caller, I could check if the returned record is null, but PL/SQL complains the if-statement that
PLS-00306: wrong number or types of arguments in call to 'IS NOT NULL'
Here is my code:
v_record my_table%ROWTYPE;
v_row_id my_table.row_id%TYPE := 123456;
begin
v_record := myfunction(v_row_id)
if (v_record is not null) then
-- do something
end if;
end;
function myfunction(p_row_id in my_table.row_id%TYPE) return my_table%ROWTYPE is
v_record_out my_table%ROWTYPE := null;
begin
select * into v_record_out from my_table
where row_id = p_row_id;
return v_record_out;
end myfunction;
Thanks.
As far as I know, it's not possible. Checking the PRIMARY KEY or a NOT NULL column should be sufficient though.
You can check for v_record.row_id IS NULL.
Your function would throw a NO_DATA_FOUND exception though, when no record is found.
You can't test for the non-existence of this variable so there are two ways to go about it. Check for the existence of a single element. I don't like this as it means if anything changes your code no longer works. Instead why not just raise an exception when there's no data there:
I realise that the others in the exception is highly naughty but it'll only really catch my table disappearing when it shouldn't and nothing else.
v_record my_table%ROWTYPE;
v_row_id my_table.row_id%TYPE := 123456;
begin
v_record := myfunction(v_row_id)
exception when others then
-- do something
end;
function myfunction(p_row_id in my_table.row_id%TYPE) return my_table%ROWTYPE is
v_record_out my_table%ROWTYPE := null;
cursor c_record_out(c_row_id char) is
select *
from my_table
where row_id = p_row_id;
begin
open c_record_out(p_row_id);
fetch c_record_out into v_record_out;
if c_record_out%NOTFOUND then
raise_application_error(-20001,'no data);
end if;
close c_record_out;
return v_record_out;
end myfunction;

Resources