Beruflich Dokumente
Kultur Dokumente
Though SQL, is the natural language of the DBA, it suffers from various
inherent disadvantages, when used as a conventional programming language.
** No Procedural capabilities
** Decrease the speed of data processing
** No facility of programmed handling of errors.
Although SQL is a very powerful tool, it’s set of disadvantages are eliminated
in PL/SQL.
SQL
PL/SQL
SQL Plus
• It also enables you to type in and execute PL/SQL code and is one of
the most common front ends used to develop and create stored
PL/SQL procedures and functions.
Relationship of
1
302:RDBMS [MES-BCA] PLSQL BLOCK
2
302:RDBMS [MES-BCA] PLSQL BLOCK
PL/SQL Datatypes
PL/SQL Datatypes
• SCALAR TYPE :
• Seven Families OF Scalar Type :
• NUMERIC FAMILY
• CHARACTER FAMILY
• DATE FAMILY
• BOOLEAN FAMILY
• RAW FAMILY
• ROWID FAMILY
• TRUSTED FAMILY
PL/SQL Datatypes
• NUMERIC FAMILY :
• BINARY_INTEGER
3
302:RDBMS [MES-BCA] PLSQL BLOCK
• DEC
• DOUBLE PRECISSION
• INTEGER
• NATURAL
• NUMBER
• POSITIVE
• REAL
• SMALLINT etc.
4
302:RDBMS [MES-BCA] PLSQL BLOCK
IF Statement :
IF <Condition> THEN
<Action>
END IF;
Conditional control in PL/SQL
Syntax :
[1] Simple IF Statement
IF <Condition> THEN
<Action>
END IF;
[2] IF-THEN-ELSE
IF <Condition> THEN
<Action>
ELSE
<Action>
END IF;
[3] IF-THEN-ELSIF
IF <Condition> THEN
<Action>
ELSIF <Condition> THEN
<Action>
ELSE
<Action>
END IF;
5
302:RDBMS [MES-BCA] PLSQL BLOCK
The simplest form of LOOP statement is the basic (or infinite) loop. which
encloses a sequence of statements between the keywords LOOP and END
LOOP.
Syntax :
LOOP
Sequence_of_Statements;
END LOOP;
LOOP Statement
The WHILE-LOOP
Syntax :
WHILE <Condition>
LOOP
<Action>
END LOOP;
WHILE-LOOP
FOR LOOP
Syntax :
FOR-LOOP
A transaction begins with the first executable SQL statement after a commit,
rollback or connection made to the Oracle engine.
ORACLE TRANSACTIONS
6
302:RDBMS [MES-BCA] PLSQL BLOCK
** Connecting to Oracle
** Disconnecting from Oracle
** Committing to the database table
** Rollback
Closing Transaction
A transaction can be closed by using either a commit or a rollback statement.
By using these statements, table data can be changed or all the changes made
to the table data undone.
ORACLE TRANSACTIONS
Using COMMIT :
ORACLE TRANSACTIONS
Using ROLLBACK :
ORACLE TRANSACTIONS
Creating SAVEPOINT
SAVEPOINT marks and saves the current point in the processing of a
transaction. When a SAVEPOINT is used with a ROLLBACK statement, parts
of a transaction can be undone.
An active savepoint is one that is specified since the last COMMIT or
ROLLBACK.
Syntax :
SQL> SAVEPOINT savepoint ;
ORACLE TRANSACTIONS
7
302:RDBMS [MES-BCA] PLSQL BLOCK
WHAT IS A CURSOR?
The Oracle Engine uses a work are for its internal processing in order
to execute an SQL statement. This work area is private to SQL’s operations
and is called a Cursor.
TYPES OF CURSOR
Cursors are classified depending on the circumstances under which they are
opened.
8
302:RDBMS [MES-BCA] PLSQL BLOCK
Implicit Cursor
The Oracle engine implicitly opens a cursor on the server to process each SQL
statement.
Implicit cursor attributes can be used to access information about the status
of last insert, update, delete or single-row select statement.s. This can be done
by preceding the implicit cursor attribute with the cursor name (i. e. SQL).
The values of the cursor attributes always refer to the most recently executed
SQL statement, whatever the statement appears.
[1] %ISOPEN
The Oracle engine automatically opens and closes the SQL cursor after
executing its associated select, insert, update or delete SQL statement has
been processed in case of implicit cursors.
Syntax : SQL%ISOPEN
[2] %FOUND
Syntax : SQL%FOUND
[3] %NOTFOUND
Syntax : SQL%NOTFOUND
9
302:RDBMS [MES-BCA] PLSQL BLOCK
[4] %ROWCOUNT
Syntax : SQL%ROWCOUNT
Syntax : cursorname%ISOPEN
[2] %FOUND
Returns the number of rows fetched from the active set. It is set to zero
when the cursor is opened.
Syntax :
CURSOR cursorname IS
10
302:RDBMS [MES-BCA] PLSQL BLOCK
Syntax :
OPEN cursorname;
How to Fetch a record from the Cursor?
Using FETCH statement, to fetch the record from the cursor.
Syntax :
CLOSE cursorname;
Exception Section
When an SQL sentence fails the Oracle engine is the first to recognize
this as an Exception Condition. The Oracle engine immediately tries to
handle the exception condition and resolve it. This is done by raising a built-
in Exception Handler.
DUP_VAL_ON_INDEX
LOGIN_DENIED
NO_DATA_FOUND
NOT_LOGGED_ON
PROGRAM_ERROR
TIMEOUT_ON_RESOURCE
TOO_MANY_ROWS
VALUE_ERROR
OTHERS
11
302:RDBMS [MES-BCA] PLSQL BLOCK
DECLARE
exception_name EXCEPTION
BEGIN
EXCEPTION
WHEN <Exception name> THEN
{User defined to be taken};
END;
12
302:RDBMS [MES-BCA] PLSQL BLOCK
Parameter modes
IN - It is considered read only and cannot be changed.
OUT - It can be read or written to, but while reading NULL.
IN OUT – It can be read or written to.
13
302:RDBMS [MES-BCA] PLSQL BLOCK
14
302:RDBMS [MES-BCA] PLSQL BLOCK
Objectives:
Package Specification
Contains information about the contents of the package.
It does not contain the code for any subprograms.
The elements within the package are the same as they would be in the
declarative section of an anonymous block.
Package body
The package body is a separate data dictionary object from the package
header (specification).
It cannot be successfully compiled unless the package header has
already been successfully compiled.
The body contains the code for the subprograms declared in package
specification.
Overloading subprograms
Procedures and functions can be overload inside package.
Restrictions on overloading
Subprograms cannot be overloaded if their parameters differ only in
name or mode.
Two functions cannot be overloaded based only on their return type.
15
302:RDBMS [MES-BCA] PLSQL BLOCK
16
302:RDBMS [MES-BCA] PLSQL BLOCK
Triggers
The triggering event can be a DML operation on a database table or
views.
Triggers can be also fired on system events such as database startup
and shutdown, and certain kinds of DDL operations.
Triggers are used for maintaining complex integrity constraints not
possible through declarative constraints enabled at table creation.
Triggers
They are used for auditing information in a table by recording the
changes made and who made them.
Automatically signaling other programs that action needs to take place
when changes are made to a table.
Publishing information about various events in a publish-subscribe
environment.
Kinds of triggers
There are three kinds of triggers:
1) DML
2) Instead of
17
302:RDBMS [MES-BCA] PLSQL BLOCK
3) System trigger
18
302:RDBMS [MES-BCA] PLSQL BLOCK
TRIGGER SYNTAX
CREATE OR REPLACE TRIGGER trigger_name
{ BEFORE | AFTER | INSTEAD OF }
triggering_event
[referencing_clause]
[ WHEN trigger_condition ]
[FOR EACH ROW ]
trigger_body;
19
302:RDBMS [MES-BCA] PLSQL BLOCK
Triggering predicates
INSERTING : INSERTING is TRUE if the triggering statement is an
INSERT: FALSE otherwise.
UPDATING : UPDATING is TRUE if the triggering statement is an
UPDATE: FALSE otherwise.
DELETING : DELETING is TRUE if the triggering statement is an
DELETE: FALSE otherwise.
EXAMPLE
IF INSERTING THEN
-------------
ELSIF UPDATING THEN
-------------
ELSE
-------------
END IF;
Instead-Of triggers
Instead-Of triggers are fired instead of DML operation on views on
which trigger is defined.
They are used in two cases:
1) To allow a view that would otherwise not be modifiable to be
modified.
2) To modify the columns of a nested table column in a view.
EXAMPLE: DML TRIGGER
/* WRITE DATABASE TRIGGER THAT CHECK THE FOLLOWING :
1) THE QTY TO BE DISPATCHED IS AVAILABLE IN PRODUCT_MASTER
TABLE.
2) THE QTY BEING DISPATCHED,IS NOT EQUAL TO ZERO AND
3) THE QTY BEING DISPATCHED IS LESS THAN OR EQUAL TO THE
SALES_ORDER */
21
302:RDBMS [MES-BCA] PLSQL BLOCK
SALES_ORDER,SALES_ORDER_DETAILS,CHALLAN_HEADER,CHALL
AN_DETAILS TABLES.*/
V_NEW_QTY NUMBER(8);
V_QTY_ON_HAND NUMBER(8);
V_QTY_ORDERED NUMBER(8);
V_QTY_DISP NUMBER(8);
V_PRODUCTNO VARCHAR2(6);
V_CHALLAN_NO VARCHAR2(6);
BEGIN
V_NEW_QTY := :NEW.QTY_DISP;
END;
OUTPUT :-
SQL> INSERT INTO CHALLAN_DETAILS
2 VALUES('CH0018','P0345',0);
INSERT INTO CHALLAN_DETAILS
*
ERROR at line 1:
ORA-20001: QUANTITY DISPATCHED CAN NOT BE 0
ORA-06512: at "SCOTT.QTY_CHECK", line 15
ORA-04088: error during execution of trigger 'SCOTT.QTY_CHECK'
EXAMPLE:
/* Write a DATABASE TRIGGER that allows changes to
CLIENT_MASTER table only
during the business hours( i.e. 8 A.M. to 5 P.M.) From MONDAY to
SATURDAY.
There is no restriction on viewing data from the table.
*/
CREATE OR REPLACE TRIGGER time_check
BEFORE INSERT OR UPDATE OR DELETE ON CLIENT_MASTER
BEGIN
IF ( TO_NUMBER(TO_CHAR(SYSDATE,'HH24') ) < 8 OR
TO_NUMBER(TO_CHAR(SYSDATE,'HH24') ) >= 17 OR
TO_CHAR(SYSDATE,'DAY') = 'SUN' ) THEN
RAISE_APPLICATION_ERROR(-20000,'Changes t o EMPMAST
table
allowed only during business hours');
END IF;
END;
23