can anybody explain what is the main difference between functions and procedures in Oracle? Why must I use procedures if I can do everything with functions?
- If I cannot call procedure in sql statement, ok, I'll write a function to do the same work.
- Procedures don't return values, ok, I'll return only sql%rowcount or 1(success), 0(exception) after any dml operation
- Both procedures and functions can pass variables to calling environment via OUT/IN OUT parameters
I heard that the main difference is in performance, 'procedures are faster than functions'. But without any detail.
Thanks in advance.
i think the major difference is :
Functions can not contain DML Statemnt whereas the procedures can. for example like Update and Insert.
if i am wrong correct me
There is almost never a performance difference between procedures and functions.
In a few extremely rare cases:
IN OUT
argument is faster than a function return, when inlining is enabled.IN OUT
argument is slower than a function return, when inlining is disabled.Test code
Results
The above code is trivial and perhaps subject to other optimizations. But I have seen similar results with production code.
Why the difference doesn't matter
Don't look at the above test and think "a procedure runs twice as fast as a function!". Yes, the overhead of a function is almost twice as much as the overhead of a procedure. But either way, the overhead is irrelevantly small.
The key to database performance is to do as much work as possible in SQL statements, in batches. If a program calls a function or procedure ten million times per second then that program has serious design problems.
The difference is- A function must return a value (of any type) by default definition of it, whereas in case of a procedure you need to use parameters like
OUT
orIN OUT
parameters to get the results. You can use a function in a normalSQL
where as you cannot use a procedure inSQL
statements.Some Differences between Functions and Procedures
A function always returns a value using the return statement while a procedure may return one or more values through parameters or may not return at all.Although,
OUT
parameters can still be used in functions, they are not advisable neither are there cases where one might find a need to do so. UsingOUT
parameter restricts a function from being used in a SQL Statement.Functions can be used in typical SQL statements like
SELECT
,INSERT
,UPDATE
,DELETE
,MERGE
, while procedures can't.Functions are normally used for computations where as procedures are normally used for executing business logic.
Oracle provides the provision of creating "Function Based Indexes" to improve the performance of the subsequent SQL statement. This applies when performing the function on an indexed column in where clause of a query.
More Information on Functions Vs. Procedures here and here.
State-changing vs non-state-changing
On top of Romo Daneghyan's answer, I've always viewed the difference as their behaviour on the program state. That is, conceptually,
Ie, if you called a function named
generateId(...)
, you'd expect it to only do some computation and return a value. But calling a proceduregenerateId ...
, you might expect it to change values in some tables.Of course, it seems like in Oracle as well as many languages, this does not apply and is not enforced, so perhaps it's just me.
As I know, Store procedure is compiled once and can be called again and again without compiled again. But function is compiled each time called. So, Store procedure improves performance than function.
Procedure may or may not return value but functions return value.
procedure use out parameter returnvalue purpose but function returnstatment provide.