Chapter-12 Getting Started With PL/SQL: SQL Vs PL/SQL: Limitations of SQL Are
Chapter-12 Getting Started With PL/SQL: SQL Vs PL/SQL: Limitations of SQL Are
in
CHAPTER-12
GETTING STARTED WITH PL/SQL
SQL Vs PL/SQL:
• No procedural capabilities .
• Time Consuming Processing or Network traffic.
• No Error Handling Routines/Procedures.
• Procedural Capabilities.
• Reduced Network Traffic.
• Error Handling Procedures/Routines.
• Facilitates Sharing.
• Improved Transaction Performance.
• Portable Code.
ANCHORED DECLARTION:
It refers to a declaration where a variable is declared with another variable or a table column used
as its anchor.
PL/SQL use %TYPE declaration attribute for anchoring.
Note: Anchored types are evaluated at compile time.Thus,you need to recompile the change of
underlying type in the anchored variable.
DECLARE
/* definitions of <constants>
<variables>
BEGIN
<PL/SQL statement here>
[EXCEPTION]
<Exception Handling>
END;
TYPES OF BLOCKS:
• Anonymous Blocks: Blocks without headers.
• Named Blocks: Blocks having headers or labels like procedure,functions,packages or
triggers.
1. Simple IF:-
Syntax:
IF <condition>THEN
Statement
END IF;
Example:
DECLARE
a number;
BEGIN
a :=&a;
if a>100 THEN
dbms_output.put_line(a);
END IF;
2. IF…THEN…ELSE…END IF:-
Syntax:
IF <condition>THEN
Statement1;
ELSE
Statement2;
END IF;
Example:
DECLARE
a number;
b number;
BEGIN
a :=&a;
b :=&b;
if a>b THEN
dbms_output.put_line(a);
ELSE
dbms_output.put_line(b);
END IF;
3. NESTED IF ….ELSE:-
IF <condition>THEN
Statement1;
ELSIF <condition>
Statement2;
THEN
.
.
.
ELSE
END IF;
Example:
DECLARE
a number;
b number;
c number;
BEGIN
a :=&a;
b :=&b;
c :=&c;
if a>b THEN
if a>c THEN
dbms_output.put_line(a);
ELSE
dbms_output.put_line(c);
END IF;
ELSE
if (b>c) THEN
dbms_output.put_line(b);
ELSE
dbms_output.put_line(c);
END IF;
END IF:
4. ELSIF LADDER:-
Example:
DECLARE
salary number;
BEGIN
salary :=&salary;
if salary >=10000 THEN
dbms_output.put_line(“CLASS I OFFICER”);
ELSIF salary <10000 AND salary>=8000 THEN
dbms_output.put_line(“CLASS II OFFICER”);
ELSIF salary <8000 AND salary>=5000 THEN
dbms_output.put_line(“CLASS III OFFICER”);
ELSE
dbms_output.put_line(“YOU ARE NOT IN JOB”);
END IF;
END IF;
NOTE: Simple loop does not terminate by itself.So EXIT and EXIT WHEN
statements are used with it to terminate the loop.
Ex: DECLARE
count number :=0;
BEGIN
LOOP
count :=count +1;
dbms_output.put_line(‘value of count is’||count);
IF count >=10 THEN
EXIT;
END IF;
END LOOP:
dbms_output.put_line(‘Hi,I m out of the loop’);
END;
Ex: DECLARE
count number :=0;
BEGIN
LOOP
count :=count +1;
dbms_output.put_line(‘value of count is’||count);
EXIT WHEN count>=10 ;
END LOOP:
dbms_output.put_line(‘Hi,I m out of the loop’);
END;
Ex:
BEGIN
FOR num IN 1..20
LOOP
n := num*2;
dbms_output.put_line(n);
END LOOP;
END;
Ex:
BEGIN
FOR num IN REVERSE 1..20
LOOP
n := num*2;
dbms_output.put_line(n);
END LOOP;
END;
Ex:
DECLARE
n number;
BEGIN
WHILE n<=10
LOOP
n := n+1;
dbms_output.put_line(n);
END LOOP;
END;
WHILE TRUE
LOOP
<executable statement>
END LOOP;
LABELLING LOOPS:
Loops can be labeled to enhance readability.
Syntax:
<<outer loop>>
LOOP
.
.
EXIT WHEN condition;
END LOOP outer loop;
SELECT,INSERT,UPDATE,DELETE.
The above syntax is used when we want to store some particular fields or columns of SQL into
PL/SQL variables.
But what if we wish to store entire row of data into PL/SQL variable, in that situation the concept
of records is used.
USING RECORDS:
Types of Records:
a. Table based records.
b. Programmer based records.
c. Cursor based records.
Here,RECORD TYPE declared is treated as a data type,which can not hold values.For which we need to
declare a variable of that type.
Syntax:
Variablename RECORD type;
EXCEPTIONS are some unwanted or undesired situations,which terminate the PL/SQL script
unexpectedly.
Types Of EXCEPTIONS:
1. Predefined Exceptions.
2. Undefined Exceptions.
3. User-defined Exceptions.
Predefined Exceptions are not needed to be declared and raised while Userdefined Exceptions are to be
declared,raised and handled in EXCEPTION handling section.
***