Unit-4 Advanced SQL
Unit-4 Advanced SQL
Unit-4 Advanced SQL
Advanced PL/SQL Cursors – Implicit cursor – Explicit cursor – Triggers – Advantages - creating trigger – raising trigger -
Advantages of Exceptions – predefined Exceptions – user defined Exceptions .
A cursor is a pointer to this the work area or context area, used by the oracle engine for executing SQL statements. . PL/SQL
controls the context area through a cursor. A cursor holds the rows (one or more) returned by a SQL statement. The set of rows the cursor holds is
referred to as the active set.
There are two types of cursors −
Implicit cursors
Explicit cursors
Implicit Cursors are controlled by Oracle Engine hence programmers cannot access its information.
When a DML statement is executed an implicit cursor is created and attached to it.
To work with the cursor whether Implicit or Explicit cursor, there are following attributes which are used:
ATTRIBUTE DESCRIPTION
NAME
%ISOPEN If cursor is open it returns a Boolean value TRUE otherwise it
returns Boolean value FALSE
%FOUND If records fetched by cursor was successful it returns Boolean
value TRUE otherwise it returns Boolean value FALSE
%NOTFOUND If records fetched by cursor was unsuccessful it returns
Boolean value TRUE otherwise it returns Boolean
value FALSE
%ROWCOUNT It returns the number of rows affected by PL/SQL statement
The following program will update the table and increase the salary of each customer by 500 and use the SQL
%ROWCOUNT attribute to determine the number of rows affected
SQL> DECLARE
2 total_rows number(2);
3 BEGIN
4 UPDATE emp
5 SET sal = sal + 1000;
Declaring the cursor defines the cursor with a name and the associated SELECT statement. For example −
CURSOR c_customers IS
SELECT id, name, address FROM customers;
2.Opening the cursor:Opening the cursor for allocating the memory(it ready for fetching the rows returned by the SQL
statement into I)t
OPEN <cursor_name>; OPEN c_customers;
3.Fetching the Cursor(for retrieving the data) Fetching the cursor involves accessing one row at a time.
fetch <cursor_name> into <list_of_variables>;
FETCH c_customers INTO c_id, c_name, c_addr;
4.losing the cursor (to release the allocated memory)
CLOSE cursorname;
TRIGGERS are stored programs that are fired(called by Oracle engine automatically when DML Statements like insert,
update, delete are executed on the table or some events occur. You can choose the event upon which the trigger
needs to be fired and the timing of the execution. The purpose of trigger is to maintain the integrity of information on
the database. a trigger can be invoked when a row is ins
Advantages of Triggers
Following are the benefits of triggers.
STATEMENT level Trigger: It fires one time for the specified event statement.
ROW level Trigger: It fires for each record that got affected in the specified event. (only for DML)
Create DATABASE Trigger: It fires when the database event is specified (LOGON/LOGOFF/STARTUP/SHUTDOWN)
CREATE OR REPLACE TRIGGER is a keyword used to create a trigger and <trigger_name> is user-defined where a trigger
can be given a name.
BEFORE/AFTER/INSTEAD OF specify the timing of the trigger's occurance. INSTEAD OF is used when a view is created.
REFERENCING is a keyword used to provide reference to old and new values for DML statements.
FOR EACH ROW is the clause used to specify row level tigger.
WHEN is a clause used to specify condition to be applied and is only applicable for row-level trigger.
DECLARE, BEGIN, EXCEPTION, END are the different sections of PL/SQL code block containing variable declaration,
executable statements, error handling statements and marking end of PL/SQL block respectively where DECLARE and
EXCEPTION part are optional.
Create trigger:
Let's take a program to create a row level trigger for the CUSTOMERS table that would fire for INSERT or UPDATE or DELETE operations performed on
the CUSTOMERS table. This trigger will display the salary difference between the old values and new values:
After the execution of the above code at SQL Prompt, it produces the following result.
Trigger created.
Use the following code to get the old salary, new salary and salary difference after the trigger created.
Exception-Handling
Exceptions will stop the program from executing further, so to avoid such condition, they need to be captured and
handled separately. This process is called as Exception-Handling, in which the programmer handles the exception
that can occur at the run time.
Syntax:
fiBEGIN
<execution block>
.
.
EXCEPTION
WHEN <exceptionl_name>
THEN
<Exception handling code for the “exception 1 _name’' >
WHEN OTHERS
THEN
<Default exception handling code for all exceptions >
END;
c clas
It is possible to handle potential errors from many statements by using a single exception handler.
If user needs to check for error at every point, it is solved by adding an exception handler to PL/SQL block in the
program.
To check error at a specific spot is possible by enclosing a single statement or a group of statements inside its own
exception handler.
Isolating error handling method makes the rest of the program easier to read and understand
o User-defined Exceptions
o System-defined Exceptions
PL/SQL facilitates their users to define their own exceptions according to the need of the program. A user-defined
exception can be raised explicitly, using either a RAISE statement or the procedure
DBMS_STANDARD.RAISE_APPLICATION_ERROR.
DECLARE
my-exception EXCEPTION;
SQL>edit user_expDECLARE
myex EXCEPTION;
i NUMBER;BEGIN
FOR i IN (SELECT * FROM enum) LOOP
IF i.eno = 3 THEN
RAISE myex;
END IF;
END LOOP;
EXCEPTION
WHEN myex THEN
dbms_output.put.line('Employee number already exist in enum table.');END;/
Result
SQL>@user_exp
Employee number already exist in enum table.
2. PL/SQL Pre-defined Exceptions: There are many pre-defined exception in PL/SQL which are executed when any
database rule is violated by the programs.For example: NO_DATA_FOUND is a pre-defined exception which is raised
when a SELECT INTO statement returns no rows.