Beruflich Dokumente
Kultur Dokumente
%ROWTYPE
The %ROWTYPE attribute provides a record type that represents a row in a table (or
view). The record can store an entire row of data selected from the table or fetched from a
cursor or strongly typed cursor variable.
EXCEPTION
An Exception is raised when an error occurs. In case of an error then normal execution
stops and the
control is immediately transferred to the exception handling part of the PL/SQL Block.
Exceptions are designed for runtime handling, rather than compile time handling.
Exceptions improve
readability by letting you isolate error-handling routines.
When an error occurs, an exception is raised. That is, normal execution stops and control
transfers to
the exception-handling part of your PL/SQL block or subprogram. Internal exceptions are
raised
implicitly (automatically) by the runtime system. User-defined exceptions must be raised
explicitly by
RAISE statements, which can also raise predefined exceptions.
To handle raised exceptions, you write separate routines called exception handlers. After
an exception
handler runs, the current block stops executing and the enclosing block resumes with the
next
work
area.
Usage - If the SELECT statement returns more that one row then explicit cursor should
be
used.
Steps
Declare a cursor
Open a cursor
%NOTFOUND (Returns true if the cursor does not contain any value)
Parameterized Cursor
A cursor can take parameters, which can appear in the associated query wherever
constants can
appear. The formal parameters of a cursor must be IN parameters. Therefore, they cannot
return
values to actual parameters. Also, you cannot impose the constraint NOT NULL on a
cursor parameter.
The values of cursor parameters are used by the associated query when the cursor is
opened.
B .IMPLICIT CURSOR
An IMPLICIT cursor is associated with any SQL DML statement that does not have a
explicit
cursor associated with it.
This includes:
ORACLE 9I
Include the FOR UPDATE clause in the cursor query to lock the row first.
Use the WHERE CURRENT OF clause to refer the current row from an explicit cursor.
Syntax
WHERE CURRENT OF < cursor_name >
Cursor_Name -It is the name of a declared cursor. [ The cursor have been
declared with the FOR UPDATE clause]
Example of Using FOR WHERE CURRENT OF Clause
1.
DECLARE
CURSOR EMP_CURSOR IS SELECT EMPNO, ENAME, DNAME FROM
E.EMP,D.DEPT WHERE
EMP.DEPTNO=DEPT.DEPTNO AND EMP.DEPTNONO=80 FOR UPDATE OF
SALARY NOWAIT;
BEGIN
FOR I IN EMP_CURSOR;
LOOP
IF I.SAL<5000 THEN
UPDATE EMP SET SALARY=I.SAL*1.10 WHERE CURRENT OF EMP_CURSOR;
END IF;
END LOOP;
END;
6
[The Example loops through each employee in department 80 , and checks whether the
salary is
less than 5000.If salary is less than , the salary is raised by 10%. The where current of
clause in the
UPDATE statement refers to the currently fetched records. ]
3. CURSORS WITH SUB QUERIES
Sub queries are often used in the WHERE clause of select statement. It can be used to
FROM clause, creating a temporary data source for the query.
DECLAREbonus REAL;
BEGIN
FOR emp_rec IN (SELECT empno, sal, comm FROM emp)
LOOP
bonus := (emp_rec.sal * 0.05) + (emp_rec.comm * 0.25);
INSERT INTO bonuses VALUES (emp_rec.empno, bonus);
END LOOP;
COMMIT;
END;
7
PROCEDURES
Procedure is a subprogram that contains set of SQL and PL/SQL statements.
Merits
Reusability Subprograms once executed can be used in any number of applications
Maintainability - Subprogram can simplify maintenance, subprogram will be affected
only
its definition changes
Parameter
IN The IN parameter is used to pass values to a subprogram when it is invoked
OUT The OUT parameter is used to return values to the caller of a subprogram
IN OUT
The IN OUT parameter is used to pass initial values to the subprogram
when invoked and also it returns updated values to the caller.
Using the NOCOPY Compiler Hint
Suppose a subprogram declares an IN parameter, an OUT parameter, and an IN OUT
parameter. When you call the subprogram, the IN parameter is passed by reference. That
is, a pointer to the IN actual parameter is passed to the corresponding formal parameter.
So, both parameters reference the same memory location, which holds the value of the
actual parameter.
By default, the OUT and IN OUT parameters are passed by value. That is, the value of
the IN OUT
actual parameter is copied into the corresponding formal parameter. Then, if the
subprogram exits
normally, the values assigned to the OUT and IN OUT formal parameters are copied into
the
corresponding actual parameters.
When the parameters hold large data structures such as collections, records, and instances
of object
types, all this copying slows down execution and uses up memory. To prevent that, you
can specify the
NOCOPY hint, which allows the PL/SQL compiler to pass OUT and IN OUT parameters
by reference.
In the following example, you ask the compiler to pass IN OUT parameter my_staff by
reference
instead of by value:
DECLARE
TYPE Staff IS VARRAY(200) OF Employee;
PROCEDURE reorganize (my_staff IN OUT NOCOPY Staff) IS ...
Remember, NOCOPY is a hint, not a directive. So, the compiler might pass my_staff by
value despite
your request. Usually, however, NOCOPY succeeds. So, it can benefit any PL/SQL
application that
passes around large data structures.
In the example below, 25000 records are loaded into a local nested table, which is passed
to two local procedures that do nothing but execute NULL statements. However, a call to
one procedure takes 21 seconds because of all the copying. With NOCOPY, a call to the
other procedure takes much less than 1 second.
SQL> SET SERVEROUTPUT ON
SQL> GET test.sql
1 DECLARE
2
TYPE EmpTabTyp IS TABLE OF emp%ROWTYPE;
3
emp_tab EmpTabTyp := EmpTabTyp(NULL); -- initialize
4
t1 NUMBER(5);
5
t2 NUMBER(5);
6
t3 NUMBER(5);
7 PROCEDURE get_time (t OUT NUMBER) IS
8
BEGIN
SELECT TO_CHAR(SYSDATE,SSSSS) INTO t FROM dual;
END;
9 PROCEDURE do_nothing1 (tab IN OUT EmpTabTyp) IS
10
BEGIN
NULL;
END;
11 PROCEDURE do_nothing2 (tab IN OUT NOCOPY EmpTabTyp) IS
8
12
BEGIN
NULL;
END;
13 BEGIN
14
SELECT * INTO emp_tab(1) FROM emp WHERE empno = 7788;
15
emp_tab.EXTEND(24999, 1); -- copy element 1 into 2..25000
16
get_time(t1);
17
do_nothing1(emp_tab); -- pass IN OUT parameter
18
get_time(t2);
19
do_nothing2(emp_tab); -- pass IN OUT NOCOPY parameter
20
get_time(t3);
21
DBMS_OUTPUT.PUT_LINE(Call Duration (secs));
22
DBMS_OUTPUT.PUT_LINE(--------------------);
23
DBMS_OUTPUT.PUT_LINE(Just IN OUT: || TO_CHAR(t2 - t1));
24
FUNCTIONS