Professional Documents
Culture Documents
The PL/SQL Language: IF Then Else While Loop FOR Loop Exit When Goto
The PL/SQL Language: IF Then Else While Loop FOR Loop Exit When Goto
The PL/SQL Language: IF Then Else While Loop FOR Loop Exit When Goto
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.
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];
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];
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.
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.