PLSQL Overview
PLSQL Overview
By
Parteek Bhatia
Assistant Professor
Dept of Comp Sc & Engg
Thapar University
Patiala
Procedural Language/Structured Query
Language
PL/SQL is an extension of the SQL language.
It eliminates many restrictions of the SQL Language.
PL/SQL extends SQL by adding control structures
found in the other procedural languages.
Procedural constructs blend seamlessly with Oracle
SQL, resulting in a structured, powerful language.
PL/SQL combines the SQL’s language’s ease of
data manipulation and the procedural language’s
ease of programming.
PL/SQL Engine
A PL/SQL
Block
Procedural
A PL/SQL
Statement
Block Procedural Executor
S
Q
L
Declare
age number (4) ;
Done Boolean ;
Three ways:
i) With Assignment operator
a:=b*c;
increase : = sal * 1.5 ;
OK : = false ;
ii) With substitute variables
a:=&enter_number;
b:=&b;
iii) With Select into statement
Select col_name into var_name where cond;
Workshop on Advanced Databases,
Chitkara Univ, By Parteek Bhatia
Variable Value Assignment
%type
DECLARE
eno emp.emp_no%type;
%rowtype
rec emp%rowtype;
DBMS_OUTPUT.PUT_LINE(‘ENTER THE
NUMBER’);
DBMS_OUTPUT.PUT_LINE(‘Result of Sum
operation is: ’ || sum);
To display messages to the user the
SERVEROUTPUT should be set to ON. Here,
DBMS_OUTPUT is a package name and PUT_LINE
is a function.
Syntax:
SET SERVEROUTPUT [ON/OFF];
Workshop on Advanced Databases,
Chitkara Univ, By Parteek Bhatia
Control structures of PL/SQL
Conditional Control
Iterative Control
Sequential Control
IF-THEN STATEMENT
IF- THEN – ELSE STATEMENT
IF-THEN-ELSIF STATEMENT (LADDER IF)
IF a > b THEN
dbms_ouput.put_line(‘a is greater’);
END IF;
IF A > B THEN
DBMS_OUTPUT.PUT_LINE(‘ A IS GREATER ‘);
ELSE
DBMS_OUTPUT.PUT_LINE(‘ B IS GREATER ‘);
END IF;
DECLARE
A NUMBER=&A;
B NUMBER:=&B;
C NUMBER;
X NUMBER;
BEGN
X:=&ENTER_CHOICE;
IF X=1 THEN
C:=A+B;
ELSIF X=2 THEN
C:=A-B;
ELSIF X=2 THEN
C:=A-B;
ELSIF X=3 THEN Workshop on Advanced Databases,
Chitkara Univ, By Parteek Bhatia
C:=A*B;
ELSIF X=4 THEN
C:=A/B;
ELSE
DBMS_OUTPUT.PUT_LINE(‘NOT A VALID OPTION’);
END IF;
DBMS_OUTPUT.PUT_LINE(‘RESULT IS’ || C);
END;
<<outer>>
LOOP
...
LOOP
...
EXIT outer WHEN ... /* exit both loops, if we only use EXIT WHEN
without label it exit only the inner loop */
END LOOP;
...
END LOOP outer;
DECLARE
ctr INTEGER;
BEGIN
...
FOR ctr IN 1..25 LOOP
...
IF ctr > 10 THEN
... -- refers to loop counter
END LOOP;
END;
<<main>>
DECLARE
ctr INTEGER;
BEGIN
………..
FOR ctr IN 1..25 LOOP
IF main.ctr > 10 THEN
... -- refers to global variable
END LOOP;
END main;
Workshop on Advanced Databases,
Chitkara Univ, By Parteek Bhatia
Sequential Control
GOTO statement
NULL statement
GOTO Statement
BEGIN GOTO insert_row;
………….
<<insert_row>> INSERT INTO emp VALUES
...
END;
Solution is:
LOOP
…
IF done THEN
GOTO end_loop;
END IF;
--
<<end_loop>> -- illegal
NULL; -- an executable statement
END LOOP; -- not an executable statement
END;