Sunteți pe pagina 1din 2

http://it.toolbox.

com/blogs/oracle-guide/learn-plsql-procedures-and-functions-13030 Procedures VS Functions Procedures are traditionally the workhorse of the coding world and functions are traditionally the smaller, more specific pieces of code. In general, if you need to update the chart of accounts, you would write a procedure. If you need to retrieve the organization code for a particular GL account, you would write a function. Here are a few more differences between a procedure and a function:

A function MUST return a value A procedure cannot return a value Procedures and functions can both return data in OUT and IN OUT parameters The return statement in a function returns control to the calling program and returns the results of the function The return statement of a procedure returns control to the calling program and cannot return a value Functions can be called from SQL, procedure cannot Functions are considered expressions, procedure are not

That's about all the differences I can think of off the top of my head. Can you think of any more? Parameters Named PL/SQL programs (procedures and functions) can take parameters. Parameters are named variables that are available to a program and that modify program behavior and/or data. Parameters are optional on both procedures and functions. Parameters are declared when a procedure or function are declared and are declared between an open and a close parenthesis (()). Parameters may be named anything that follows Oracle naming standards. Keep them under 30 characters, they must start with a letter and contain no spaces. There additional rules but those are the ones that are most commonly violated. There are three types of parameter: IN, OUT and IN OUT. An IN parameter is used an input only. An IN parameter cannot be changed by the called program. An OUT parameter is initially NULL. The program assigns the parameter a value and that value is returned to the calling program.

An IN OUT parameter may or may not have an initial value. That initial value may or may not be modified by the called program. Any changes made to the parameter are returned to the calling program. Parameters are declared with data types but without data type length or precision. That means that a parameter may be declared as VARCHAR2 but it will not be declared with a length component (VARCHAR2(30) would not be valid). Parameters may also be assigned a default value. You can either use the assignment operator (:=) or use the DEFAULT keyword. When a parameter has a default value, you do not need to include that parameter in the call. You MUST always include IN OUT and OUT parameters. Sample parameter declarations:
( parameter_1 IN VARCHAR2 := 'ABC', parameter_2 IN VARCHAR2 DEFAULT 'ABC', parameter_3 IN OUT NUMBER, parameters_can_be_named_anything OUT DATE

Parameter Reference Calling programs may refer to parameters either positionally or by named notation. It is considered a best practice to refer to parameters via named notation. Almost no one does this consistently though (myself included). Positional notation refers to listing the parameters by the position that they are declared. Using the example above, we could positionally refer to the parameters as: ('def', 'ghi', v_number_variable, v_date_variable) For Named notation, you would use the actual name of the parameter. This is very handy for large parameter lists and for those parameter lists that have plenty of default values. When using named notation, the parameters do not have to be listed in the order of declaration. (parameter_3 => v_number_variable, parameter_1 => 'def', v_date_variable => v_date_variable) Procedures The procedure is a program that performs an action and does not return a value (outside of IN OUT and OUT parameters). A procedure is declared as:

S-ar putea să vă placă și