I have created a PL/SQL function that is supposed to be adding a given number of records to a table with the help of new_dept_new
sequence that I've created earlier. The function compiled successfully. But I'm getting an error when calling the function:
BEGIN
dbms_output.put_line(myfunction3(3, 'OPERATIONS', 'NEW YORK'));
END;
Error report -
ORA-06503: PL/SQL: Function returned without value
The function:
CREATE OR REPLACE FUNCTION myfunction3(
records_count_in IN NUMBER, dept_name_in IN dept.dname%TYPE, loc_name_in IN dept.loc%TYPE)
RETURN INTERVAL DAY TO SECOND
IS
thestart TIMESTAMP := CURRENT_TIMESTAMP;
stopwatch INTERVAL DAY TO SECOND;
records_count NUMBER := records_count_in;
dept_name dept.dname%TYPE := dept_name_in;
loc_name dept.loc%TYPE := loc_name_in;
BEGIN
FOR i IN 1..records_count LOOP
INSERT INTO dept VALUES(new_dept_new.nextval, dept_name || TO_CHAR(new_dept_new.currval), loc_name || TO_CHAR(new_dept_new.currval));
COMMIT;
END LOOP;
stopwatch := CURRENT_TIMESTAMP - thestart;
DBMS_OUTPUT.PUT_LINE('Time for processing function: ');
RETURN stopwatch;
EXCEPTION WHEN OTHERS
THEN dbms_output.put_line('ERROR Processing Request for Department: ' || dept_name_in);
END;
Why is the function returned without value?
EDIT: After receiving feedback from the comments I've edited the function and now the values are inserted into the table and the timestamp value is returned. I also want an application error to be raised if 0 rows were inserted.
So I did the following:
CREATE OR REPLACE FUNCTION myfunction3(
records_count_in IN NUMBER, dept_name_in IN dept.dname%TYPE, loc_name_in IN dept.loc%TYPE)
RETURN INTERVAL DAY TO SECOND
IS
thestart TIMESTAMP := CURRENT_TIMESTAMP;
stopwatch INTERVAL DAY TO SECOND;
records_count NUMBER := records_count_in;
dept_name dept.dname%TYPE := dept_name_in;
loc_name dept.loc%TYPE := loc_name_in;
BEGIN
FOR i IN 1..records_count LOOP
INSERT INTO dept (deptno, dname, loc)
VALUES(new_dept_new.nextval, dept_name || TO_CHAR(new_dept_new.currval), loc_name || TO_CHAR(new_dept_new.currval));
IF SQL%ROWCOUNT = 0 THEN
raise_application_error (-20001, 'ERROR Processing Request for Department: ' || dept_name_in);
END IF;
END LOOP;
stopwatch := CURRENT_TIMESTAMP - thestart;
DBMS_OUTPUT.PUT_LINE('Time for processing function: ');
RETURN stopwatch;
END;
However if I'm calling the function like this and zero records are inserted, I'm not getting my custom error message.
BEGIN dbms_output.put_line(myfunction3(0, 'DEV_OPS', 'NEW YORK')); END;
How can I fix this?
exception
block. You should never, ever useWHEN OTHERS THEN
. The result is that whatever errors your function has will be ignored and the compiler will say "OK, successful compilation" when in fact the function may be full of mistakes. Don't feel bad; lots of people do what you did - but that doesn't make it any less wrong.records_count_in
as 0raise raise_application_error
. It wont' enter the loop at all. It cannot be simulated this way .SQL%ROWCOUNT
will be set to 0 only if no records were inserted.