DBMS Unit4
DBMS Unit4
DBMS Unit4
ADVANCED PL/SQL
1.CURSORS
Cursors are a private working area or a temporary working area. “It is mainly used for
processing multiple records with in the PL/SQL.
A cursor is a temporary work area created in the system memory when a SQL
statement is executed. This temporary work area is used to store the data retrieved
from the database, and manipulate this data. A cursor can hold more than one row,
but can process only one row at a time.
Cursors are mainly divided into two types in SQL. There are
Implicit Cursor Attributes: These attributes are associated with the implicit SQL cursor and
can be accessed by appending the attribute name to the implicit cursor name (SQL).
Syntax: SQL%<attribute name>;
A PL/SQL program opens a cursor to process rows returned by a query and then closes the
cursor.
Fetch / Retrieve data from the cursor: After the cursor is opened, the current row is
loading into variables. The current row is the row at which the cursor is currently pointing.
The transfer of data into PL/SQL variables is done through „FETCH‟ statement.
Explicit cursor attributes: We use these attributes to avoid errors while accessing
cursors through Open, Fetch and Close statements. There are the attributes available to
check the status of an explicit cursor.
In PL/SQL explicit cursor has 4 attributes there are:
1. %ISOPEN
2. %FOUND
3. %NOTFOUND
4.%ROWCOUNT
%ISOPEN: „True‟, if the cursor is already open in the program otherwise „False‟ the
cursor is not opened.
Syntax: <cursor_name>%ISOPEN;
Example: cur%ISOPEN;
%FOUND: „True‟, if fetch statement returns at least one row otherwise „False‟, if fetch
statement doesn‟t return a row.
Syntax: <cursor_name>%FOUND;
Syntax: <cursor_name>%ROWCOUNT;
2.TRIGGERS:
* A trigger is a procedural SQL code that is automatically invocated by the RDBMS upon the
occurrence of a given data manipulation event.
* We can make a Trigger to Fire (Executed) only for DML statements (Insert, Update, and
Deleted).
*The Trigger code is stored in the database and runs automatically whenever the Triggering
event occurs.
1. Trigger Statement: The Trigger statement specified DML statement like Insert,
Update, Delete and that causes a trigger to be fired.
2. Trigger Restriction: We can specify the condition inside trigger to when trigger is fire.
3. Trigger Body: When the trigger SQL statement is execute, trigger automatically
call and PL/SQL trigger block execute.
Syntax for Creating Trigger:
1. Before / After
2. For each Row / Statement.
Before / After: The Before / After options can be used to specify when the trigger body
should be fire with respect to trigger statement.
If Before option is used then Oracle Engine Fires the Trigger before executing the
statement.
If After option is used then Oracle Engine Fires the Trigger After executing the
Trigger statement.
for each Row/Statement:
Statement Level Trigger: A Trigger which is created only for one record are called “Statement
Level Trigger”. In practice these are not used frequently by default. Every Trigger is statement
level Trigger.
Row Level Trigger: The Trigger which is created on the total table is known as “Row Level
Trigger” by using Row Level Trigger. We can make a Trigger to fire on selected columns for
more than once. When working with Row Level Triggers. We can use two qualifiers called
“Old” & “New”. The most commonly used Triggers are Row Level Triggers.
Example: You may use a Database Trigger to log the fact that an account receivable clerk
has lowered the amount owing on an outstanding account.
Ex:
Create or replace trigger T1
before update on student
for each row
Begin
dbms_output.put_line(“record
is updated”);
End;
Enabling & Disabling Triggers: Triggers don‟t affect all rows in the table. They
affect only Transactions of the specified type. When the Triggers Enable. The Triggers will
not affect any transactions created before to a Trigger by default. Triggers are enabled
when it is created, but sometimes we may need to disable Triggers disabling Triggers
during data loads improve the performances of the data load. In such a way the data load
partially succeeded and the Triggers was executed for a portion of the data load records. It
is also possible to since a trigger twice for the same transaction. To enable a trigger “Alter
Trigger” command is used with the “enable” keyword.
Dropping Trigger: If there are any unwanted Triggers in the data base. They must be
removed from that the database, it is also called Dropping a Trigger. To drop any Trigger
the following syntax can be used.
Syntax: SQL> drop trigger <trigger name>;
Trigger dropped.
Advantages of Triggers: Triggers can be used to makes the RDBMS to take some action.
When a database related event has occurred the following advantages of Triggers.
The Business rules can be stored in the database consistently with each and
every updated operation.
The reduce complexity of the application program that the database.
3.Exception Handling:
PL/SQL makes it easy to detect and process error conditions called „Exception‟.
An exception is an error condition is raised. That is, normal execution stops and
control transfers to the exception-handling part of PL/SQL block.
For example if try to division a number by zero, the pre defined execution ZREO-
DIVIDE is raised automatically. User-define exceptions must be raised explicitly by
RASIE statement.
EXAMPLE:
User-define Exception: PL/SQL user define exception to make own exception. User-define
exception must be declare yourself and RAISE statement to raise explicitly.
Declare Exception: You must have to declare user define exception name in „Declare‟ block.
RAISE Exception: RAISE statement to raised defined exception name and control transfer to
an exception block
Implement Exception: In PL/SQL Exception block add When condition to implement user
define action.
Example:
Dbms_output.put_line(‘Commission:’,comm);
Else
Raise Vcomm;
Exception
End exception;
End;