Professional Documents
Culture Documents
Introduction to PL/SQL :
PL/SQL stands for Procedural Language SQL. PL/SQL extends SQL by adding
control structures found in other procedural language.
PL/SQL is unique as it combines the flexibility of SQL with the power and
configurability of a third generation language. The procedural constructs and
database access are present in PL/SQL.
PL/SQL can be used in the Oracle relational database in the Oracle
server and in client side application development tools.
Disadvantages of SQL:-
SQL statements are passed to the oracle engine one at a time. Each time an SQL
statement is executed, a call is made to the Engine’s Resources. This adds to the
traffic on the Network, thereby decreasing the speed of data processing.
While processing an SQL sentence if an error occurs, the Oracle Engine displays
its own error messages. SQL has no facility for programmed handling of errors
that arise during manipulation of data.
Advantages of PL/SQL:-
PL/SQL is a development tool that not only supports SQL data manipulation but
also provides facilities of conditional checking Branching and Looping
PL/SQL sends an entire block of statements to the Oracle engine at one time.
The communication between the program block and the Oracle engine reduces
considerably which in turn reduces network traffic. Oracle engine gets the SQL
statements as a single block, and hence it processes this code much faster than if
it got the code one sentence at a time. Result is that there is definite
improvement in the performance time of the Oracle engine
Via., PL/SQL, all sorts of calculations can be done quickly and efficiently
without the use of the Oracle engine. This considerably improves transaction
performance.
ORACLE Server
Host program
Or
Oracle Tool
PL/SQL Engine
Procedural
PL/SQL Statement
PL/SQL Procedural
Block Executor
Engine
PL/SQL Block:-
A PL/SQL Block has a definite structure. The minimum sections of a PL/SQL block
are
The master Begin & End Section that contains the Exception Section.
DECLARE
BEGIN
EXCEPTION
END;
Data Types
Number ( )
Date
Boolean
Attributes:
Attributes allow us to refer to data types and objects from the database.
PL/SQL variables and constants can have attributes. The following are the types of
attributes supported by PL/SQL.
%type
%rowtype
%type: %type attribute is used when declaring variables that refer to the database
columns.
Eg: Declare
em emp.empno%type ;
Eg: declare
em1 emp%rowtype;
In the above example, em1 stores a row selected from the emp table.
VARIABLES:-
CONSTANTS
Syntax:-
<var> constant number;
ex: x constant number :=10;
DBMS_OUTPUT .PUT_LINE
Syntax:-
SET SERVEROUTPUT [ON/OFF]
Comments:-
The comment line begins with a Slash followed by an Asterisk (/*) till the
occurrence of an Asterisk followed by a Slash (*/)
Syntax:-
IF<Condition> THEN
<Action>
ELSIF<condition>THEN
<Action>
ELSE
<Action>
END IF;
declare
a number;
b number;
begin
a:=&a;
b:=&b;
if (a>b) then
dbms_output.put_line(‘a is big’);
else
dbms_output.put_line(‘b is big’);
end if;
end;
Step 2 : Execution
We can execute the Sql file by using the RUN Command or @ Symbol. If we
have stored the sql file in default directory then we can directly specify the file name.
If we have stored the SQL file in our own defined directory (e.g. c:\bhavans)
then we should specify the complete path along with the command.
Iterative Controls
1 . While loop
Syntax:
While <condition> loop
---------------
---------------
End loop;
SQL> ed c:\bhavans\natural.sql
natural.sql
declare
i number;
n number;
begin
i:=0;
n:=&n;
i:=i+1;
dbms_output.put_line(i);
end loop;
end;
/
Output & Execution :
SQL> @ c:\bhavans\natural.sql
Enter value for n: 4
old 6: n:=&n;
new 6: n:=4
1
2
3
4
PL/SQL procedure successfully completed.
2 .For loop
Syntax:
FOR <variable> IN [REVERSE] start..end loop
----------------
END LOOP;
Power.sql
SQL> ed c:\bhavans\power.sql
declare
power number;
b number:=&b;
e number:=&e;
i number;
begin
power:=1;
for i in 1..e loop
power:=power*b;
end loop;
dbms_output.put_line(‘The Power of the Given Expression is ||power);
end;
/
3. Loop
Syntax:
LOOP
-----------
-----------
END LOOP;
EXCEPTION HANDLING :
Advantages of Exceptions :
PREDEFINED EXPLANATION
EXCEPTION
No_data_found This exception is raised when select statement returns no
rows.
Cursor_already_open This exception is raised when we try to open a cursor
which is already opened.
DECLARE
er emp%rowtype;
BEGIN
EXCEPTION
END;
User –Defined Exceptions :
Syntax:
DECLARE
BEGIN
SQL sentence;
IF <condition> THEN
END IF;
EXCEPTION
END;
DECLARE
er emp%rowtype;
errnumber number;
errmess varchar2(100);
BEGIN
(&eno,'&name',&dno,&salary);
EXCEPTION
END;
CURSORS
The Oracle Engine uses a work area for its internal processing in order
to execute an SQL statement written in PL/SQL code. This work area is private to
SQL ‘s operations and is called a Cursor.
The data that is stored in the in the cursor is called the Active Data Set.
Types of cursor Cursors are classified as
1. IMPLICIT CURSORS
1. EXPLICIT CURSORS
1. Implicit Cursors :If the oracle engine for its internal processing has opened a
cursor they are known as implicit cursors. This can contain only one record while
retrieving data from table and this is closed immediately after the transaction is over.
General attributes : When the oracle engine creates an implicit or explicit cursor,
cursor control variables are also created to control the execution of the cursor.
Whenever any cursor is opened and used ,the oracle engine creates a set of four system
variables which keeps track of the current status of a cursor.
Both implicit and explicit cursors have four attributes. They are
BEGIN
dbms_output.put_line('records found');
else
end if;
else
end if;
END;
1. create cursor.
2. open cursor.
DECLARE
er emp%rowtype;
BEGIN
open ec;
loop
dbms_output.put_line('salary :: '||er.sal);
end loop;
close ec;
END ;
STORED PROCEDURES AND FUNCTIONS
1. A declarative part.
1. security : Stored procedures and functions can help enforce data security.
IS
Variable declarations
Constant declarations
BEGIN
EXCEPTION
END;
DATABASE TRIGGERS
The Oracle engine allows the user to define procedures that are
implicitly executed (i.e. executed by the oracle engine itself), when an insert update or
delete is issued against a table from SQL* PLUS or through an application. These
procedures are called database triggers. The major issue that makes these triggers
stand-alone is that they are fired implicitly (i.e. internally) by the Oracle engine itself
and not explicitly called by the user.
Use of Database Triggers:
1. A Trigger can permit DML statements against a table only if they are
issued, during regular business hours or on predetermined weekdays.
2. A Trigger can also be used to keep an audit trail of a table (i.e. to store
the modified and deleted records of the table.)
a) It can be used to prevent invalid transactions.
b) Enforce complex security authorizations.
When a trigger is fired, an SQL statement inside the trigger’s PL/SQL code block
can also fire the same or some other trigger. This is called cascading, triggers.
2. Trigger restriction :
A trigger restriction specifies a Boolean(logical) expression
that must be TRUE for the trigger to fire. It is an option available for triggers that
are fired for each row. Its function is to conditionally control the execution of a
trigger. A trigger restriction is specified using a WHEN clause.
3. Trigger Action :
A trigger action is the PL/SQL code to be executed when a triggering
statement is encountered
and any trigger restriction evaluates to TRUE. The PL/SQL block can contain SQL
and PL/SQL statements can define PL/SQL language constructs and call stored
procedures . Additionally for row triggers , the statements the PL/SQL block have
access to column values (: new and :old) of the current row being processed.