You are on page 1of 6

Difference between PL/SQLs FUNCTION, PROCEDURE, and PACKAGE

FUNCTION differs from the PROCEDURE in that it returns a value. Furthermore, a FUNCTION must be
part of an executable statement, as it cannot be executed independently. In order to illustrate the nature
of the FUNCTION module, let's look at the EVALUATE_SIZE function:

CREATE OR REPLACE FUNCTION EVALUATE_SIZE(GIVEN_SIZE IN NUMBER) RETURN VARCHAR2 IS


BEGIN
IF GIVEN_SIZE < 30 THEN RETURN 'Low';
ELSIF GIVEN_SIZE < 60 THEN RETURN 'Medium';
ELSE RETURN 'High';
END IF;
END EVALUATE_SIZE;
/
The EVALUATE_SIZE function will return a string response based on the provided number input. So, in a
typical scenario we would use the EVALUATE_SIZE within an executable statement in the following
manner:

SQL> SELECT CONTRACT_DATE, METHOD, EVALUATE_SIZE(MONTHLY_COST) "Cost" FROM CONTRACTS


WHERE ROWNUM = 1;

CONTRACT_DATE METHOD Cost


------------- ------ -----
15-DEC-09 CHECK High

PROCEDURE represents an independent executable statement. It differs from the FUNCTION module
in that it does not return a value (i.e. in the same manner). You might notice that PROCEDURE has a
RETURN statement; but, that statement does not take any value, as it is only used to return control to
the calling code. To return a value from a PROCEDURE you would need to use the OUT or IN
OUT parameters (do note that this approach is also appicable to functions). So, when would you use
a PROCEDURE and when would you use a FUNCTION? Well, a PROCEDURE would be used for performing
complex business logic that requires seveal interactions with the RDBMS; wehereas a FUNCTION would
be used for performing a very specialized task, e.g. formatting input and/or output. With that said, here's
an example of a PROCEDUREwhich calculates and displays student GPAs:

SET SERVEROUTPUT ON
CREATE OR REPLACE PROCEDURE display_student_gpa IS
CURSOR c_calculate_student_gpa IS
SELECT
s_id,
s_first,
s_last,
TO_CHAR((SUM( credits * TO_NUMBER(
DECODE( grade, 'A', '4', 'B', '3', 'C', '2', 'D',
'1', 'F', '0' ) ) ) / SUM( credits ) ), 9.99) AS "GPA"
FROM student
LEFT OUTER JOIN enrollment USING(s_id)
JOIN course_section USING(c_sec_id)
JOIN course USING(course_no)
WHERE grade IS NOT NULL
GROUP BY s_id, s_first, s_last;
student_record c_calculate_student_gpa%ROWTYPE;
BEGIN
OPEN c_calculate_student_gpa;
LOOP
FETCH c_calculate_student_gpa INTO student_record;
EXIT WHEN c_calculate_student_gpa%NOTFOUND;
DBMS_OUTPUT.PUT_LINE( 'Student: ' || student_record.s_first || ' ' ||
student_record.s_last );
DBMS_OUTPUT.PUT_LINE( 'ID: ' || student_record.s_id );
DBMS_OUTPUT.PUT_LINE( 'GPA: ' || student_record.gpa );
END LOOP;
CLOSE c_calculate_student_gpa;
END display_student_gpa;
/

Once the display_student_gpa procedure is created or replaced, by running it (with the adequate
database structure and data) we will get the following output:

SQL> CALL display_student_gpa();


Student: Lisa Johnson
ID: JO101
GPA: 2.33
Student: Tammy Jones
ID: JO100
GPA: 3.50
Student: John Marsh
ID: MA100
GPA: 2.00
Student: Jorge Perez
ID: PE100
GPA: 3.00

Call completed.

REMEMBER THE BELOW POINTS REGARDING FUNCTION vs PROCEDURE:

1. Function is mainly used in the case where it must return a value. Where as a procedure may or may
not return a value or may return more than one value using the OUT parameter.

2. Function can be called from SQL statements where as procedure can not be called from the sql
statements

3. Functions are normally used for computations where as procedures are normally used for executing
business logic.

4. You can have DML (insert,update, delete) statements in a function. But, you cannot call such a function
in a SQL query.

5. Function returns 1 value only. Procedure can return multiple values (max 1024).

6.Stored Procedure: supports deferred name resolution. Example while writing a stored procedure that
uses table named tabl1 and tabl2 etc..but actually not exists in database is allowed only in during creation
but runtime throws error Function wont support deferred name resolution.

7.Stored procedure returns always integer value by default zero. where as function return type could be
scalar or table or table values

8. Stored procedure is precompiled execution plan where as functions are not.

9.A procedure may modify an object where a function can only return a value The RETURN statement
immediately completes the execution of a subprogram and returns control to the caller.
NOTE:

1. we can call a stored procedure inside stored Procedure,Function within function


,StoredProcedure within function but we cannot call function within stored procedure.

2. we can call function inside select statement only when your function is deterministict..
3. We can return value from function without passing output parameter as a parameter to the
stored procedure.

PACKAGE provides you with the abilitiy to structure and organize your functions and procedures based
on their business logic. By using packages you ease the maintenance and improve the overall
performance of your application.

For further information on the application of PL/SQL's FUNCTION, PROCEDURE, and PACKAGE I highly
recommend Oracle PL/SQL Programming by Steven Feuerstein and Bill Pribyl. They discuss in quite detail
various aspects of PL/SQL features, and provide practical examples as well.

PL/SQL cryptic warning: Procedure created with compilation


errors
Many times PL/SQL wont tell you what is wrong with your procedure and it will just print this cryptic
message:

Warning: Procedure created with compilation errors.


If you dont see any error in your code, use the following command:

SHOW ERRORS PROCEDURE <procedure_name>;


It will output something like which makes it much easier to debug the procedure:

SQL> SHOW ERRORS PROCEDURE add_book


Errors for PROCEDURE ADD_BOOK:

LINE/COL ERROR
-------- ---------------------------------------------
--------------------
28/3 PL/SQL: SQL Statement ignored
29/20 PL/SQL: ORA-00984: column not allowed here
You can use similar command not only with procedures:
SHOW ERRORS FUNCTION
SHOW ERRORS PACKAGE
SHOW ERRORS PACKAGE BODY
SHOW ERRORS TRIGGER
SHOW ERRORS VIEW
etc

PACKAGE IN ORACLE
The following package specification and package body declare and define 2 procedures. First the
procedure is used to insert new records into the table DEPT table(here mydept table), where the second
procedure deletes a record from it.

SQL> select * from mydept;

DEPTNO DNAME LOC


---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON

First we will create the package and run it.

CREATE OR REPLACE PACKAGE deptpack as

procedure addrec(deptno number,dname varchar2,loc varchar2);

procedure delrec(dno number);

end deptpack;
OUTPUT:-

Package created.

Now we will create package body and will run it:-

CREATE OR REPLACE PACKAGE body deptpack as

procedure addrec(deptno number,


dname varchar2,
loc varchar2) is
begin
insert into mydept values(deptno,dname,loc);
end addrec;

procedure delrec(dno number) is


begin
delete from mydept where deptno=dno;
end delrec;

end deptpack;

OUTPUT:-
Package body created.

Now we will run the package.

SQL> exec deptpack.addrec(50,'marketing','London');

PL/SQL procedure successfully completed.

SQL> select * from mydept;


/

DEPTNO DNAME LOC


---------- -------------- ------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
50 marketing London

SQL> exec deptpack.delrec(50);

PL/SQL procedure successfully completed.

SQL> select * from mydept;


/

DEPTNO DNAME LOC


---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON