I need to to select id, name and annual compensation.
I am running the script and everything is okay. I am asking to show me the result for id=100 and everything is perfect.
My problem is - if I want to check for id=300 (which is not in the table) I need to get result 'No employee with that ID' but it returns me 'no rows selected'. Is there something wrong with my last SELECT or with the EXCEPTION?
RETURN NUMBER IS
v_sal employees.salary%TYPE;
v_comm employees.commission_pct%TYPE;
BEGIN
SELECT salary,commission_pct INTO v_sal,v_comm
FROM employees
WHERE employee_id=v_empid;
RETURN (NVL(v_sal,0) * 12 + (NVL(v_comm,0) * NVL(v_sal,0) * 12));
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee with that ID');
END get_annual_emp;
SELECT employee_id,last_name,
get_annual_emp(employee_id) "Annual Compensation"
FROM employees
WHERE employee_id=&v_empid;
Looks like exception handler doesn't do its job as you presumed it will.
A function is supposed to return some value. You're just displaying a message (on the screen, using dbms_output.put_line - that's not visible elsewhere, in a tool that doesn't support it).
Therefore, you could return null:
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee with that ID');
RETURN null;
END get_annual_emp;
Or, raise an error:
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20000, 'No employee with that ID');
END get_annual_emp;
You can't actually return "No employee with that ID" as return value because it is a string, and your function returns a number. You could modify return value's datatype to varchar2, but - you do expect a number, don't you?
Related
I have this anonymous PL/SQL block which calculates and prints a value return from a table.
DECLARE
U_ID NUMBER :=39;
RETAIL BINARY_FLOAT:=1;
FLAG NUMBER;
BEGIN
SELECT NVL(RETAIL_AMOUNT,1),UNIT_ID INTO RETAIL, FLAG FROM UNITS WHERE UNIT_ID=U_ID;
LOOP
SELECT NVL(MAX(UNIT_ID),U_ID) INTO FLAG FROM UNITS WHERE FATHER_ID=FLAG;
IF FLAG=U_ID THEN EXIT; END IF;
SELECT RETAIL* RETAIL_AMOUNT INTO RETAIL FROM UNITS WHERE UNIT_ID=FLAG;
EXIT WHEN FLAG=U_ID;
END LOOP;
DBMS_OUTPUT.PUT_LINE( RETAIL);
END;
This block work correctly, but I wanted to do the same thing using a PL/SQL Function
I wrote the function as follow:
CREATE OR REPLACE FUNCTION GET_UNIT_RETAIL(U_ID NUMBER)
RETURN NUMBER
IS
RETAIL BINARY_FLOAT:=1;
FLAG NUMBER;
BEGIN
SELECT NVL(RETAIL_AMOUNT,1),UNIT_ID
INTO RETAIL, FLAG
FROM UNITS
WHERE UNIT_ID=U_ID;
LOOP
SELECT NVL(MAX(UNIT_ID),U_ID)
INTO FLAG
FROM UNITS
WHERE FATHER_ID=FLAG;
IF FLAG=U_ID THEN
EXIT;
END IF;
SELECT RETAIL* RETAIL_AMOUNT
INTO RETAIL
FROM UNITS
WHERE UNIT_ID=FLAG;
EXIT WHEN FLAG=U_ID;
END LOOP;
RETURN NUMBER;
END;
/
When I try to execute the above code to save the function to the database, the environment (SQL*PLUS) hangs for a long time and at the end returns this error:
ERROR at line 1:
ORA-04021: timeout occurred while waiting to lock object
What is the problem ??? Please !
Sounds like ddl_lock problem
Take a look at
dba_ddl_locks to see who is "blocking" a create or replace.
Also try to create under different name - and see what happens.
The problem was because the Object GET_UNIT_RETAIL was busy by other environment
Here is the answer:
https://community.oracle.com/thread/2321256
I have package with Period and TableOfPeriod types:
TYPE Period
IS RECORD
( StartPeriod Date,
EndPeriod Date
);
TYPE TableOfPeriod
IS TABLE OF Period;
and in this package I have three simple function:
FUNCTION Temp1
RETURN TableOfPeriod IS
returnedValue TableOfPeriod := TableOfPeriod();
BEGIN
returnedValue.extend(1);
returnedValue(1).StartPeriod := sysdate-100;
returnedValue(1).EndPeriod := sysdate;
RETURN returnedValue;
END Temp1;
FUNCTION CalculateFine
return VARCHAR2
IS
freewillLockTableRP TableOfPeriod:=TableOfPeriod();
compulsoryLockTableRP TableOfPeriod:=TableOfPeriod();
BEGIN
--for testing
compulsoryLockTableRP:=Temp1();
FOR i IN compulsoryLockTableRP.FIRST..compulsoryLockTableRP.LAST LOOP
IF(((compulsoryLockTableRP(i).EndPeriod - compulsoryLockTableRP(i).StartPeriod)>1)) THEN
BEGIN
-- RAISE_APPLICATION_ERROR(-20001, 'Hello world');
SELECT T111.StartPeriod StartPeriod,
T111.EndPeriod EndPeriod
bulk collect into freewillLockTableFull
FROM TABLE(DistributeDaysByPeriods(compulsoryLockTableRP, 5)) T111;
END;
END IF;
END LOOP;
/*SELECT T111.StartPeriod StartPeriod,
T111.EndPeriod EndPeriod
-- BULK COLLECT INTO compulsoryLockTableRP
bulk collect into freewillLockTableFull
FROM TABLE(DistributeDaysByPeriods(compulsoryLockTableRP, 5)) T111;*/
--===========
--SOME OTHER PROCESSING
RETURN 'Ok '
|| '#r';
EXCEPTION
WHEN No_Data_Found THEN return 'No data found#g';
-- WHEN OTHERS THEN RETURN SQLERRM;
END CalculateFine;
When I execute this function, I have next error:
"ORA-21700: object does not exist or is marked for delete ORA-06512:
at "MyPackageName", line 1181 ORA-06512: at line 1
21700. 00000 - "object does not exist or is marked for delete""
where 1181 line is a line with Select statement of CalculateFine function. Can anybody tell me, whats wrong and how I can solve this problem?
Check if you have in the same folder where your package is located a file with the same name but with an extention *.~sql (e.g.: your_file_name.~sql). I had the same error but after I have deleted an *.~sql file I could compile my package without ORA errors.
Here is my sql code for a function that calculates the area of a circle. I am having trouble getting a dbms_output to show the user defined exception.
I begin by declaring an exception underzero. then i raise it if the user inputs a number less than or equal to 0. In the exception i have dbms_output then return the result. It shows in the query result that the value is null, however the output isn't showing.
I have already set serveroutput on and set verify off. I don't know why it wont output anything. If i run that line alone it outputs to the dbms output window but not when the exception is raised.
create or replace function circle_area
(p_radius number)
return number
is
c_Pi Constant number := acos(-1);
v_result number(10, 2);
underzero exception;
begin
if p_radius <=0 then
raise underzero;
else
v_result := c_pi * p_radius * p_radius;
return v_result;
end if;
exception
when underzero then
dbms_output.put_line('enter number greater than 0');
return v_result;
when others then
dbms_output.put_line('Exception Location: Anonymous Block');
dbms_output.put_line(sqlcode || ': ' || sqlerrm);
return v_result;
end circle_area;
to call the function:
select circle_area(-2) from dual;
I'm not sure that I understand the problem. The code you posted will generate output to dbms_output if serveroutput is enabled...
SQL> set serveroutput on;
SQL> create or replace function circle_area
2 (p_radius number)
3 return number
4 is
5 c_Pi Constant number := acos(-1);
6 v_result number(10, 2);
7 underzero exception;
8 begin
9 if p_radius <=0 then
10 raise underzero;
11 else
12 v_result := c_pi * p_radius * p_radius;
13 return v_result;
14 end if;
15
16 exception
17 when underzero then
18 dbms_output.put_line('enter number greater than 0');
19 return v_result;
20 when others then
21 dbms_output.put_line('Exception Location: Anonymous Block');
22 dbms_output.put_line(sqlcode || ': ' || sqlerrm);
23 return v_result;
24 end circle_area;
25 /
Function created.
SQL> select circle_area(-2) from dual;
CIRCLE_AREA(-2)
---------------
enter number greater than 0
I'm assuming that this is part of a homework assignment and not a real problem that you're facing. In reality, you'd never write code whose primary purpose was to write to dbms_output and you would never, ever have an exception handler that only wrote to dbms_output.
I have multiple stored procedures calling multiple stored procedures in my database. To give a small example, I've constructed a fictionalised version of a few of them below. In my example, a Java program calls calculate_bill, which calls calculate_commission, which calls update_record.
I'm hoping to get some advice on how best to propagate the error messages up the stack to the calling Java layer, so the user gets a precise error message corresponding to wherever the error has occurred.
I'm really quite stuck on this. I've played around in my example with raise_application_error and just continually shuffling it up the stack. Is the way I'm doing it below remotely correct? Or is one raise_application_error in the relevant procedure enough, with no pragma exception init etc needed?
To give an idea of what I mean, in the example below, if a user entered a number which corresponded to a record which couldn't be updated because it didn't exist, I'd like them to get the message:
"Error calculating bill. Error calculating commission. No record exists to be updated" or something to that effect.
So two questions:
What is the best practice, most efficient, most tidy way to pass error messages up the stack for the end user in the application layer?
Does anyone have any suggestions on tidier output from the code, i.e. the best way to concatenate these errors to make them more meaningful? I'm really open to any suggestions on how to make this work best as I have absolutely no prior experience in this.
Example:
(Error in code):
-20000 : Error in top level procedure
-20001 : Error in middle level procedure
-20002 : Error in bottom level procedure
Java code:
try {
// call calculate_bill
exception (SQLException ex)
// output oracle code and relevant message.
Oracle code:
create or replace procedure calculate_bill(in_num NUMBER)
is
error_calculating_commission EXCEPTION;
error_updating_record EXCEPTION;
PRAGMA EXCEPTION_INIT (error_calculating_commission, -20001);
PRAGMA EXCEPTION_INIT (error_updating_record , -20002);
begin
if in_num > 2 then
calculate_commission(in_num);
else
raise_application_error(-20000, 'Error calculating bill. ' || 'Record number doesn''t exist.', false);
end if;
exception
when error_calculating_commission then
raise_application_error(SQLCODE, 'Error calculating bill. ' || SQLERRM, false);
when error_updating_record then
raise_application_error(SQLCODE, 'Error calculating bill. ' || SQLERRM, false);
when others then
raise_application_error(-20000, 'Unknown error encountered calculating bill.', false);
end;
create or replace procedure calculate_commission(in_num NUMBER)
is
begin
if in_num < 30 then
raise_application_error(-20001, 'Number too small to calculate commission.', false);
elsif in_num >= 30 and < 40 then
declare
error_storing_record EXCEPTION;
PRAGMA EXCEPTION_INIT (error_storing_record , -20002);
begin
update_record(in_num);
exception
when error_storing_record then
raise_application_error(SQLCODE, 'Error calculating commission. ' || SQLERRM, false);
when others then
raise_application_error(-20001, 'Unknown error encountered calculating commission.', false);
else
raise_application_error(-20001, 'Number too large to calculate commission', false);
end if;
end;
create or replace procedure update_record(in_num NUMBER)
is
begin
//some SQL query with a where clause, where in_num equals something
exception
when no_data_found then
raise_application_error(-20002, 'No record exists to be updated', false);
when others then
raise_application_error(-20002, 'Unknown error encountered updating record.', false);
end if;
end;
Note: I know this example is a little contrived. I was just trying to keep it brief.
The way I would implement this is to use RAISE_APPLICATION_ERROR where the error actually originates, then leave it unhandled in the other layers (or, if you want to do logging in the database, catch it in the OTHERS section, then re-raise using RAISE rather than RAISE_APPLICATION_ERROR.
You're OTHERS sections are still perpetuating the problem raised in your previous question: when an unknown error occurs you're replacing it with a generic, unhelpful message. To paraphrase Tom Kyte, "a WHEN OTHERS that does not end in RAISE is a bug`.
With no logging in the database, I would re-write the provided code like this:
create or replace procedure calculate_bill(in_num NUMBER)
is
begin
if in_num > 2 then
calculate_commission(in_num);
else
raise_application_error(-20000, 'Error calculating bill. ' || 'Record number doesn''t exist.', false);
end if;
end;
create or replace procedure calculate_commission(in_num NUMBER)
is
begin
if in_num < 30 then
raise_application_error(-20001, 'Number too small to calculate commission.', false);
elsif in_num >= 30 and in_num < 40 then
update_record(in_num);
else
raise_application_error(-20001, 'Number too large to calculate commission', false);
end if;
end;
create or replace procedure update_record(in_num NUMBER)
is
v_stub number;
begin
select 1 into v_stub from dual where 1 = 0;
exception
when no_data_found then
raise_application_error(-20002, 'No record exists to be updated', false);
end;
Below is sample input and the stack traces they created:
exec calculate_bill(0)
ORA-20000: Error calculating bill. Record number doesn't exist.
ORA-06512: at "SCHEMANAME.CALCULATE_BILL", line 7
ORA-06512: at line 1
exec calculate_bill(10)
ORA-20001: Number too small to calculate commission.
ORA-06512: at "SCHEMANAME.CALCULATE_COMMISSION", line 5
ORA-06512: at "SCHEMANAME.CALCULATE_BILL", line 5
ORA-06512: at line 1
exec calculate_bill(35)
ORA-20002: No record exists to be updated
ORA-06512: at "SCHEMANAME.UPDATE_RECORD", line 8
ORA-06512: at "SCHEMANAME.CALCULATE_COMMISSION", line 7
ORA-06512: at "SCHEMANAME.CALCULATE_BILL", line 5
ORA-06512: at line 1
exec calculate_bill(100)
ORA-20001: Number too large to calculate commission
ORA-06512: at "SCHEMANAME.CALCULATE_COMMISSION", line 9
ORA-06512: at "SCHEMANAME.CALCULATE_BILL", line 5
ORA-06512: at line 1
If you were to add error logging to these procedures, it would be a simple matter of adding an OTHERS clause to the EXCEPTION sections:
WHEN OTHERS THEN
my_logging (SQLCODE, SQLERRM, DBMS_UTILITY.format_error_backtrace ());
RAISE;
The new exception created by your RAISE_APPLICATION_ERROR calls will be handled transparently by the RAISE.
i've created an procedure which increases salaries of peoples working in a department by a certain rate. the department number and the rate are assed as parameters for the procedure.
Now, the procedure works great when i specify the good departement number, but once i specify a false one, and i'm waiting for the NO_DATA_FOUND Exception to raise, it never happen. I searched tried many thing but didn't found the answer, so if you can help me i woul be really greatfull. Thank you !
Here is my code :
create or replace PROCEDURE AugmenteSalaire(numDepartement in departements.numerodepartement%TYPE, taux IN number) IS
p_nbreTotalSalaire employes.salaireemploye%type;
begin
SELECT sum(salaireemploye)
INTO p_nbreTotalSalaire
FROM employes
WHERE numerodepartement = numDepartement;
if taux > 0 AND taux <= 100 THEN
if p_nbreTotalSalaire < 150000 THEN
Update employes e
SET e.salaireemploye = e.salaireemploye + (e.salaireemploye * (taux * 0.01))
WHERE e.numerodepartement = numDepartement
AND NOT numeroemploye = (SELECT departements.chefdepartement from departements
WHERE departements.numerodepartement = numDepartement);
ELSE
DBMS_OUTPUT.PUT_LINE('Transaction refused : The salary sum cannot be above 150000');
END IF;
ELSE
DBMS_OUTPUT.PUT_LINE('The rate cannot be under 0 or aboce 100');
END IF;
Exception
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('The department number provided is invalid, '||
'please enter a valid department number(DEP001 for example)');
end;
SELECT sum(salaireemploye)
INTO p_nbreTotalSalaire
FROM employes
WHERE numerodepartement = numDepartement;
In this query you are using SUM(). Now if there is no matching department, i.e. input "numDepartement" is a false department, sum(salaireemploye) is 0 (no such department, so nothing to add, so 0).
sum(salaireemploye) thus has a valid value, which is 0 here. Hence this will never raise a NO_DATA_FOUND exception