Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between function and procedure in PL/SQL?

What is the difference between function and procedure in PL/SQL ?

like image 733
Isabel Jinson Avatar asked Apr 21 '09 10:04

Isabel Jinson


People also ask

What's the difference between procedure and function?

A procedure performs a task, whereas a function produces information. Functions differ from procedures in that functions return values, unlike procedures which do not. However, parameters can be passed to both procedures and functions.

What is the difference between PL SQL function and PL SQL procedure Mcq?

Functions can be called from SQL whereas procedures cannot be called. Function can be used in place of an expression whereas a procedure can't be used so. Functions are used for executing business logic and computation but a procedure is not.

What is the difference between procedure and function in Oracle?

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 or IN OUT parameters to get the results. You can use a function in a normal SQL where as you cannot use a procedure in SQL statements.

What is procedure and function in PL SQL explain with example?

"A procedures or function is a group or set of SQL and PL/SQL statements that perform a specific task." A function and procedure is a named PL/SQL Block which is similar . The major difference between a procedure and a function is, a function must always return a value, but a procedure may or may not return a value.


2 Answers

A procedure does not have a return value, whereas a function has.

Example:

CREATE OR REPLACE PROCEDURE my_proc    (p_name IN VARCHAR2 := 'John') as begin ... end  CREATE OR REPLACE FUNCTION my_func    (p_name IN VARCHAR2 := 'John') return varchar2 as begin ... end 

Notice how the function has a return clause between the parameter list and the "as" keyword. This means that it is expected to have the last statement inside the body of the function read something like:

return(my_varchar2_local_variable); 

Where my_varchar2_local_variable is some varchar2 that should be returned by that function.

like image 155
Petros Avatar answered Oct 04 '22 15:10

Petros


A function can be in-lined into a SQL statement, e.g.

select foo       ,fn_bar (foo)   from foobar 

Which cannot be done with a stored procedure. The architecture of the query optimiser limits what can be done with functions in this context, requiring that they are pure (i.e. the same inputs always produce the same output). This restricts what can be done in the function, but allows it to be used in-line in the query if it is defined to be "pure".

Otherwise, a function (not necessarily deterministic) can return a variable or a result set. In the case of a function returning a result set, you can join it against some other selection in a query. However, you cannot use a non-deterministic function like this in a correlated subquery as the optimiser cannot predict what sort of result set will be returned (this is computationally intractable, like the halting problem).

like image 37
ConcernedOfTunbridgeWells Avatar answered Oct 04 '22 16:10

ConcernedOfTunbridgeWells