Beruflich Dokumente
Kultur Dokumente
search
ORACLE
(72 members)
forum
polls
members
view profile
SQL Material
Home > Communities > Computers & Internet > ORACLE > Forum > Messages
first | < previous | next > | last showing 31-40 of 65
10 Oct
▐►♥ ఓయ్™ ♥◄▌
Multiple statements can be added to a procedure body by adding a compound statement. Within a
compound statement, any number of SQL statements can be specified. In addition, SQL variables,
cursors, and handlers can be declared.
The following example takes as input the department number. It returns the total salary of all the
employees in that department and the number of employees in that department who get a bonus.
10 Oct
▐►♥ ఓయ్™ ♥◄▌
10 Oct
▐►♥ ఓయ్™ ♥◄▌
If the handler for SQLEXCEPTION is not specified and an error occurs that is not handled in another
handler, execution of the compound statement is terminated and the error is returned in the SQLCA.
Similar to indicators, the SQLCA is always returned from SQL procedures.
o Includes an OPEN, FETCH, and CLOSE of cursor C1. If a CLOSE of the cursor is not specified, the
cursor is closed at the end of the compound statement since SET RESULT SETS is not specified in the
CREATE PROCEDURE statement.
10 Oct
▐►♥ ఓయ్™ ♥◄▌
o Includes a WHILE statement which loops until the last record is fetched. For each row retrieved, the
TOTAL_SALARY is incremented and, if the employee's bonus is more than 0, the BONUS_CNT is
incremented.
o Returns DEPT_SALARY and DEPT_BONUS_CNT as output parameters.
Compound statements can be made atomic so if an error occurs that is not expected, the statements
within the atomic statement are rolled back. The atomic compound statements are implemented using
SAVEPOINTS. If the compound statement is successful, the transaction is committed. For more
information about using SAVEPOINTS, see Savepoints.
The following example takes as input the department number. It ensures the EMPLOYEE_BONUS table
exists, and inserts the name of all employees in the department who get a bonus. The procedure returns
the total count of all employees who get a bonus.
10 Oct
▐►♥ ఓయ్™ ♥◄▌
*
o
An SQL variable name can be qualified with the label name of the FOR statement or compound
statement in which it is defined. In the example, FOR_1.BONUS refers to the SQL variable that contains
the value of column BONUS for each row selected. CS1.NAME is the variable NAME defined in the
compound statement with the beginning label CS1. Parameter names can also be qualified with the
procedure name. CREATE_BONUS_TABLE.DEPT_NUMBER is the DEPT_NUMBER parameter for the
procedure CREATE_BONUS_TABLE. If unqualified SQL variable names are used in SQL statements
where column names are also allowed, and the variable name is the same as a column name, the name
will be used to refer to the column.
You can also use dynamic SQL in an SQL procedure. The following example creates a table that contains
all employees in a specific department. The department number is passed as input to the procedure and
is concatenated to the table name.
10 Oct
▐►♥ ఓయ్™ ♥◄▌
If the procedure is called passing value 'D21' for the department, table DEPT_D21_T is created and the
table is initialized with all the employees that are in department 'D21'.
10 Oct
▐►♥ ఓయ్™ ♥◄▌
Savepoints
Savepoints allow you to create milestones within a transaction. If the transaction rolls back, changes are
undone back to the specified savepoint, rather than to the beginning of the transaction. A savepoint is set
by using the SAVEPOINT SQL statement. For example, create a savepoint called STOP_HERE:
SAVEPOINT STOP_HERE
ON ROLLBACK RETAIN CURSORS
Program logic in the application dictates whether the savepoint name is reused as the application
progresses, or if the savepoint name denotes a unique milestone in the application that should not be
reused.
If the savepoint represents a unique milestone that should not be moved with another SAVEPOINT
statement, specify the UNIQUE keyword. This prevents the accidental reuse of the name that can occur
by invoking a stored procedure that uses the identical savepoint name in a SAVEPOINT statement.
However, if the SAVEPOINT statement is used in a loop, then the UNIQUE keyword should not be used.
The following SQL statement sets a unique savepoint named START_OVER.
This application logic books airline reservations on a preferred date, then books hotel reservations. If the
hotel is unavailable, it rolls back the airline reservations and then repeats the process for another date. Up
to 3 dates are tried.
10 Oct
▐►♥ ఓయ్™ ♥◄▌
got_reservations =0;
EXEC SQL SAVEPOINT START_OVER UNIQUE ON ROLLBACK RETAIN CURSORS;
if (SQLCODE != 0) return;
Savepoints are released using the RELEASE SAVEPOINT statement. If a RELEASE SAVEPOINT
statement is not used to explicitly release a savepoint, it is released at the end of the current savepoint
level or at the end of the transaction. The following statement releases savepoint START_OVER.