Professional Documents
Culture Documents
Advance PL/SQL
By,
Asmatullah Khan,
CL/CP, GIOE,
Secunderabad.
Outline
1. Explain cursor attributes and cursor management
2. Explain database triggers
3. Explain the concept of stored sub programs with
examples
4. List atleast five advantages of packages.
5. Explain the specifications of packaging.
6. Explain overloading and calling packaged sub
programs.
PL/SQL Cursor – pointer to a context area
• Oracle creates a memory area, known as
context area(A cursor is a pointer to this
Attribute Description
context area), for processing an SQL
statement, which contains all information Returns TRUE if an INSERT, UPDATE, or
needed for processing the statement, for DELETE statement affected one or more
example, number of rows processed, etc. %FOUND rows or a SELECT INTO statement
returned one or more rows. Otherwise, it
• PL/SQL controls the context area through returns FALSE.
a cursor.
The logical opposite of %FOUND. It
• A cursor holds the rows (one or more) returns TRUE if an INSERT, UPDATE, or
returned by a SQL statement. The set of %NOTFOUND DELETE statement affected no rows, or a
rows the cursor holds is referred to as SELECT INTO statement returned no
the active set. rows. Otherwise, it returns FALSE.
• You can name a cursor so that it could be Always returns FALSE for implicit
referred to in a program to fetch and cursors, because Oracle closes the SQL
process the rows returned by the SQL %ISOPEN
cursor automatically after executing its
statement, one at a time.
associated SQL statement.
• An explicit cursor should be defined in the declaration section of the PL/SQL Block.
The triggers are executed procedures that are fired implicitly (internally) by the oracle
engine and not explicitly by the user.
PL/SQL Triggers Benefits of Triggers
• Triggers are stored programs, which are •Generating some derived
automatically executed or fired when column values automatically
some events occur; such in response to the
following events: •Enforcing referential
integrity
▫ A database manipulation (DML) statement •Event logging and storing
(DELETE, INSERT, or UPDATE). information on table access
•Auditing
▫ A database definition (DDL) statement
(CREATE, ALTER, or DROP). •Synchronous replication of
tables
▫ A database operation (SERVERERROR, •Imposing security
LOGON, LOGOFF, STARTUP, or
SHUTDOWN). authorizations
•Preventing invalid
• Triggers could be defined on the table, transactions
view, schema, or database with which the
event is associated.
Types of Triggers
1. BEFORE Trigger :
▫ BEFORE trigger execute before the
triggering DML statement (INSERT, • Combination Trigger : Combination
UPDATE, DELETE) execute. trigger are combination of two trigger
▫ Triggering SQL statement may or may not type,
execute, depending on the BEFORE trigger ▫ Before Statement Trigger :
conditions block. Trigger fire only once for each
2. AFTER Trigger : statement before the triggering DML
▫ AFTER trigger execute after the triggering statement.
DML statement (INSERT, UPDATE, ▫ Before Row Trigger :
DELETE) executed. Trigger fire for each and every record
▫ Triggering SQL statement is execute as before the triggering DML statement.
soon as followed by the code of trigger ▫ After Statement Trigger :
before performing Database operation.
Trigger fire only once for each
• ROW Trigger : statement after the triggering DML
▫ ROW trigger fire for each and every record statement executing.
which are performing INSERT, UPDATE, ▫ After Row Trigger :
DELETE from the database table. Trigger fire for each and every record
• Statement Trigger : after the triggering DML statement
▫ Statement trigger fire only once for each executing.
statement.
Syntax for Trigger Creation
• CREATE [OR REPLACE] TRIGGER trigger_name: Creates or
replaces an existing trigger with the trigger_name.
• [FOR EACH ROW]: This specifies a row level trigger, i.e., the
trigger would be executed for each row being affected. Otherwise
the trigger will execute just once when the SQL statement is
executed, which is called a table level trigger.
Output:
Mesage Current_Date
------------------------------------------------------------