Sei sulla pagina 1di 4

THE PL/SQL LANGUAGE

PL/SQL is a block-structured language offered by Oracle to facilitate the use of the Oracle RDBMS. It has the following properties and features that can be used to aid in application development:

Block structure--The block structure allows blocks to contain nested subblocks. Block declarations--Each block can have its own declarations, which means that you can logically separate functions. Variable declaration--Variables can be declared and used within a PL/SQL block. Constant declaration--Constants can be declared and referenced within a PL/SQL block. Conditional statements--PL/SQL allows for conditional processing with IF...THEN ...ELSE, WHILE...LOOP, FOR...LOOP, EXIT...WHEN, and GOTO functions.

These features make PL/SQL a powerful SQL processing language. Using PL/SQL has several major advantages over using standard SQL statements (in addition to allowing the use of stored procedures and functions). Among these are ease of use, portability, and higher performance. The primary performance difference between PL/SQL and SQL is the fact that PL/SQL statements are transmitted to Oracle as a block of statements rather than as individual statements. In a network application, the additional overhead needed to transmit individual statements can be quite high. It takes very little additional CPU and network resources to send a larger packet than it does to send a smaller one.

PROCEDURES, FUNCTIONS, AND PACKAGES


Procedures and functions are subprograms made up of PL/SQL code that take a set of parameters given to them by the calling program and perform a set of actions. The only real difference between a procedure and a function is that a function will include a single return value. Both functions and procedures can modify and return data passed to them as a parameter. Usually, procedures are used unless only one return value is needed. A procedure or function that has been stored in the library cache is referred to as a stored procedure or a stored function. A stored procedure or stored function has the following characteristics:

It has a name--This is the name by which the stored procedure or function is called and referenced. It takes parameters--These are the values sent to the stored procedure or function from the application. It returns values--A stored procedure or function can return one or more values based on the purpose of the procedure or function. It is stored in the data dictionary--The stored procedure or function is stored in a parsed form in the data dictionary.

Procedures
A procedure is a set of PL/SQL statements that form a subprogram. The subprogram is designed and created to perform a specific operation on data in your database. A procedure

takes zero or more input parameters and returns zero or more output parameters. The syntax of a procedure is as follows: SYNTAX:
PROCEDURE procedure_name [( parameter_declaration )] IS [local declarations] BEGIN PL/SQL Statements [EXCEPTION Optional Exception Handler(s)] END [procedure_name];

In this syntax, the parameter_declaration has the following format:


parameter_name [IN | OUT | IN OUT] datatype

The parameter qualifiers have the following meanings:


IN--This parameter is used as an input value only. OUT--This parameter is used as an output value only. IN OUT--This parameter is used as both an input and an output variable.

The procedure is made up of two parts: the declaration and the body of the procedure. The declaration begins with the keyword PROCEDURE and ends with the last parameter declaration. The body begins with the keyword IS and ends with the keyword END. The declaration section is used to define which variables are passed to the procedure and which values are returned from the procedure back to the calling program. The body of the procedure is where the real work is done. The body is made up of the PL/SQL statements that perform the desired task.

Functions
A function, like a procedure, is a set of PL/SQL statements that form a subprogram. The subprogram is designed and created to perform a specific operation on data in your database. A function takes zero or more input parameters and returns just one output value. If more than one output value is required, a procedure should be used. The syntax of a function is as follows: SYNTAX:
FUNCTION function_name [( parameter_declaration )] RETURN datatype IS [local declarations] BEGIN PL/SQL Statements [EXCEPTION Optional Exception Handler(s)] END [function_name];

The parameter_declaration has the same format as it does with a procedure:


parameter_name [IN | OUT | IN OUT] datatype

The parameter qualifiers have the following meanings:


IN--This parameter is used as an input value only. OUT--This parameter is used as an output value only. IN OUT--This parameter is used as both an input and an output variable.

As with a procedure, a function is made up of two parts: the declaration and the body. The declaration begins with the keyword FUNCTION and ends with RETURN statement. The body begins with the keyword IS and ends with the keyword END.

The declaration section is used to define which variables are passed to the function and which values are returned from the function back to the calling program. The body of the function is where the real work is done. The body is made up of the PL/SQL statements that perform the desired task. The difference between a procedure and a function is the return value. A function has the return declaration as well as a RETURN function within the body of that function that returns a value. This RETURN function is used to pass a return value to the calling program. If you do not intend to return a value to the calling program, or you want to return more than one value, use a procedure.

How Procedures and Functions Operate


Procedures and functions use the same basic syntax in the program body with the exception of the RETURN keyword, which can only be used by functions. The body itself is made up of PL/SQL blocks that perform the desired function and return the desired data to the calling program. The goal of the body of the procedure is both to minimize the amount of data to be transmitted across the network (to and from the calling program) and to perform the PL/SQL statements in the most efficient manner possible. The RETURN Statement In the declaration portion of a function, a RETURN parameter is used to declare the type of the return value. Later, in the body of the function, the RETURN statement is used to exit the function and return the specified value to the calling program. With a procedure, the RETURN statement can also be used, but not to return a value. In a procedure, the RETURN statement can be used only to exit the procedure. No values can be associated with the RETURN statement in a procedure. The EXCEPTION Statement In both procedures and functions, you can add optional exception handlers. These exception handlers allow you to return additional information based on certain conditions (such as no data found or some user-specified condition). By using exception handlers and allowing the stored procedure to notify you of some special conditions, you can minimize the amount of returnvalue checking that must be done in the application code. Because the work to determine that no data has been selected has already been done by the RDBMS engine, you can save on resources if you take advantage of this information.

Packages
Packages are sets of related procedures or functions that are compiled and stored together in the data dictionary. They allow you to group together PL/SQL types, objects, and subprograms into a logical unit. When you link these logically related entities together, it can be easier to program and modify modules based on their function and relation. Performance is enhanced because the entire package is loaded into memory when it is first called, thus increasing the chance for a cache hit on a related function or object that is likely to be called soon. Packages are actually created in a statement with two different parts. The first is the declaration part, where the package is defined. Then there is the package body definition, where the body of the package is defined. The syntax of the statement used to create the package definition is as follows: SYNTAX:
CREATE PACKAGE package_name AS package_specification public type and object declaration subprogram definition

END [package_name];

This definition part of the package creation declares the parts of the package available to the user. The rest of the package definition is used by the user, but is not visible to the user. This second part has the following syntax: SYNTAX:
CREATE PACKAGE BODY package_name AS package_body private type and object declaration subprogram bodies [BEGIN initialization statements] END [package_name];

The user application must have knowledge of the package specification in order to call the package correctly. The arrangement of the package-creation process has several advantages:

Portability--The body of the package can change without requiring any changes to the application--as long as the package specification does not change. Security--The package can access tables you might not want the user to see. Because the package body is hidden from the user, some security can be maintained. Modularity--With packages, modules can have specific functions that can be logically grouped and specified. Ease of design--The specification part of the package can be completed first, thus allowing different teams to work on the package body and the application. Once the specification is completed, both groups can write to that specified interface. Better performance--Because the entire package is loaded into memory when the first component is accessed, additional calls to the package do not invoke disk I/O.

Potrebbero piacerti anche