Sie sind auf Seite 1von 7

Section 5 STORED PROCEDURES / FUNCTIONS

Stored Procedures How To Create And Execute Procedures Where To Store Procedures Stored Functions How To Create And Execute Functions Where To Store Functions Where To Procedures And Functions Reside?

STORED PROCEDURES
Procedures are named PL/SQL blocks that can take parameters, perform an action and can be invoked. A procedure is generally used to perform an action and to pass values. Procedures are made up of the following parts: A Declarative part, An Executable part. An optional exception-handling part.

Declarative part: The declarative part may contain declarations of cursors, constants, variables, exceptions, and subprograms. These objects are local to the procedure. The objects become invalid once you exit from it. Executable part: The executable part contains a PL/SQL block consisting of statements that assign values, control execution and manipulate ORACLE data. The action to be performed is coded here and data that is to be returned back to the calling environment is also returned from here. Exception Handling part: This part contains code that performs an action to deal with exceptions raised during the execution of the Executable part. This block can be used to handle Oracles own exceptions or the exceptions that are declared in the Declarative part. One cannot transfer the flow of execution from the Exception Handling part to the Executable part or vice-versa.

HOW TO CREATE & EXECUTE PROCEDURES


When a procedure is created, ORACLE automatically performs the following steps: 1. Compiles the procedure. 2. Stores the compiled code. 3. Stores the procedure in the database. The PL/SQL compiler compiles the code. If an error occurs, then the procedure is created but it is an invalid procedure. ORACLE displays a message during the time of creation that the procedure was created with compilation errors. It does not display the errors. These errors can be viewed using the select statement. SELECT * FROM user_errors;

ORACLE loads the compiled procedure in the memory area called the System Global Area (SGA). This allows the code to be executed quickly. The same procedure residing in the SGA is executed by the other users also.

EXECUTION OF PROCEDURES
ORACLE performs the following steps to execute a procedure: 1. 2. 3. Verifies user access. Verifies procedure validity. Executes the procedure.

ORACLE checks if the user who called the procedure has the execute privilege for the procedure. If the user is invalid, then access is denied otherwise Oracle proceeds to check whether the called procedure is valid or not. The user can view the validity of the procedure by using the select statement as: SELECT object_name, object_type, status FROM user_objects; WHERE object_type = PROCEDURE; Only if the status is valid, then the procedure can be executed. Once the procedure is found valid, ORACLE then loads the procedure into memory. (i.e. if it is not present in memory) and executes the PL/SQL code.

WHERE TO STORE PROCEDURES


Procedures, also known as stored procedures, are stored in the database and are invoked or called by any anonymous block (The PL/SQL block that appears within an application). Before the procedure is created, ORACLE parses the procedure. Then this parsed procedure is stored in the database.

Syntax for creating stored procedure:

CREATE OR REPLACE PROCEDURE [schema.] procedurename (argument { IN, OUT, IN OUT} datatype, ...) {IS, AS} variable declarations; constant declarations; BEGIN

PL/SQL subprogram body; EXCEPTION exception PL/SQL block; END;

STORED FUNCTIONS
Functions are named PL/SQL blocks that can take parameters, perform an action and returns a value to the host environment. A function can only return one value. Functions are made up of: 1. A declarative part, 2. An executable part, 3. And an optional exception-handling part. Declarative part: The declarative part may contain declarations of type, cursors, constant, variables, exceptions, and subprograms. These objects are local to the function. The objects become invalid once you exit from the function. Here the datatype of the return value is also declared. Executable part: The executable part contains a PL/SQL block consisting of statements that assign values, control execution, and manipulate Oracle data. The action to be performed is coded here and data that is to be returned back to the calling environment is also returned from here. Variable declared is put to use in this block. The return value is also passed back in this part. Exception handling part: This part contains code that performs an action to deal with exceptions raised during execution of the Executable Part. This block can be used to handle Oracle's own exceptions or the exceptions that are declared in the Declarative Part. One cannot transfer the flow of execution from the Exception Handling Part to the Executable Part and vice-versa. The return value can also be passed back in this part.

WHERE TO STORE FUNCTIONS


Functions in Oracle are called stored functions. Functions are stored in the database and are invoked or called by any anonymous block (a PL/SQL block that appears within an application) Before, the function is created, Oracle parses the function. Then this parsed function is stored in the database.

HOW TO CREATE & EXECUTE FUNCTIONS

When a function is created, Oracle automatically performs the following steps: 1. Compiles the function. 2. Stores the compiled code. . Stores the function in the database. The PL/SQL compiler compiles the code. If an error occurs then the function is created but its an invalid function. Oracle displays a message during the time of creation that the function was created with compilation errors. It does not display the errors. These errors can be viewed by using the select statement: SELECT * FROM user_errors; Oracle loads the compiled function in the memory area called the System Global Area (SGA). This allows the code to be executed quickly. The same function residing in the SGA is executed by the other users also.

EXECUTING A FUNCTION
Oracle performs the following steps to execute a function: 1. Verifies user access. 2. Verifies function validity. 3. Executes the function. Oracle checks if the user who called the function has the execute privilege for the function. If the user is invalid, then access is denied else if the user is valid, then it proceeds to check whether the called function is valid or not. The user can view the validity of the function by using the select statement as: SELECT object_name, object_type, status FROM user_ohjects, WHERE object_type = 'FUNCTION'; Only if the status is valid, the function can be executed. Once the function is found valid, Oracle loads the function into memory (i.e. if it is not currently present in memory) and executes the PL/SQL code.

ADVANTAGES OF FUNCTIONS
1. Security: Stored functions can help enforce data security. For e.g. you can grant users access to function that can query a table, but not grant them access to the table itself.
2.

Performance: It improves database performance in the following ways: Amount of information sent over a network is less. No compilation step is required to execute the code. As function is present in the shared pool of SGA, retrieval from disk is not required.

3. Memory Allocation: Reduction in memory as stored functions have shared memory capabilities so only one copy of function needs to be loaded for execution by multiple users. 4. Productivity: Increased development productivity, by writing a single function we can avoid redundant coding and increase productivity. 5. Integrity: Improves integrity, a function needs to be tested only once to guarantee that it returns an accurate result. So committing coding errors can be reduced.

Syntax for creating a stored function: CREATE OR REPLACE FUNCTION [schema.] functionname (argument IN datatype, ...) RETURN datatype {IS, AS} variable declarations; constant declarations; BEGIN PL/SQL subprogram body; EXCEPTION exception PL/SQL block; END;

WHERE DO PROCEDURES AND FUNCTIONS RESIDE?

For this, refer to where to store procedures and where to store functions.

Das könnte Ihnen auch gefallen