I am new in PL/SQL trying some practice examples.
I have few questions regarding PL/SQL PROCEDURE
& FUNCTION
:
When should I go for the PROCEDURE
or FUNCTION?
Means, whatever task I am doing using FUNCTION
the same task will do by using PROCEDURE
.
then why should I go for the function? is their any advantage of FUNCTION
over the PROCEDURE
in PL/SQL?
FUNCTION
must return value. Is this the only advantage for using a function or are there any other advantages of functions?
I would like to clarify that the answer for whether you should use a stored procedure or a function is completely dependent upon your business requirement and design workflow, provided you are clear about your program objective. If you are unclear about your objective, just the way your question is, no amount of coding procedures and functions would be useful.
You must note that stored procedures and functions serve different purposes in PL/SQL programming. These are as follows:
Stored procedures:
a. Stored procedures represent named blocks (as opposed to anonymous blocks) that are capable of accepting parameters and work on them.
b. Stored procedures define an independent procedural workflow where you can perform a series of DML and/or other operations.
c. Stored procedures do not have to return a value. Hence, they cannot be called from inside an SQL statement. Stored procedures must be executed from a PL/SQL block- named or anonymous.
d. Merits:
Can be simply called as an independent statement from a PL/SQL block. e.g.,
myProcedure (x, y);
e. Demerits:
SELECT
statement.Functions:
a. Functions are named blocks that are capable of accepting parameters and return a value.
b. Functions also define a procedural workflow but when used in SQL statements, you cannot perform any DML or DDL.
c. A function must be called from a SQL or PL/SQL statement where the value returned by the function is utilized- i.e., assigned to a variable, passed as a parameter, etc.
d. Merits:
SELECT
statement.e. Demerits:
For further reference, visit Oracle Docs.
A user defined function, with certain limitations, can be used in SELECT
statements and PL/SQL IF
statements whereas a PROCEDURE
cannot.
You can SELECT
from a FUNCTION
that is CAST
as a table using pipeline and PIPE ROW
statements, but that is an advanced PL/SQL feature you can use much later.
Consult the Oracle Developer documentation online as it is free and very good: Developing and Using Stored Procedures
A "value"
have be one of many things including PL/SQL tables, ref cursors etc. Adding to that, it is possible to use a function in SQL statements, whereas procedures cannot be used.OUT
or IN OUT
parameters.You can have DML(insert, update, delete) statements in a function. But, you cannot call such a function in a SQL query. *Eg: If you have a function that is updating a table, you can't call that function in any SQL query.
select myFunction(field) from sometable; --will throw error.
It is on your choice whether to use procedure or function depends on you requirement and your comfortability.
Main advantages:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With