PL SQL: Prepared By: Zeba Qureshi Assistant Professor, CSE Dept, AITR
PL SQL: Prepared By: Zeba Qureshi Assistant Professor, CSE Dept, AITR
PL SQL: Prepared By: Zeba Qureshi Assistant Professor, CSE Dept, AITR
DBMS SESSION 8
Prepared By: Zeba Qureshi
Assistant Professor, CSE Dept, AITR
PL/SQL
PL/SQL stands for Procedural Language/SQL.
For example −
• sales number(10, 2);
• name varchar2(25);
• address varchar2(100);
• name VARCHAR2(50) := ‘ZEBA';
PL/SQL Variable Constraints
NOT NULL
Can not be empty
CONSTANT
Can not be changed
PL/SQL Variables Examples with
and without Constraints
Age number;
Last char ( 10 );
DVal Date := Sysdate;
SID number not null;
Adjust constant number := 1;
CanLoop boolean := true
Program to Add 2 Numbers
Declare
Var1 integer;
Var2 integer;
Var3 integer;
Begin
Var1:=&var1;
Var2:=&var2;
Var3:=var1+var2;
Dbms_output.put_line('the sum of numbers is '|| var3);
End;
/
PL/SQL Control Structure
PL/SQL has a number of control structures which
includes:
Conditional controls
Iterative or loop controls.
Exception or error controls
declare
var1 integer;
begin
var1:=&var1;
if var1>20 then
dbms_output.put_line('Nu
mber is Greater Than 20');
end if;
end;
/
PL/SQL IF-THEN-ELSE Statement
Syntax:
IF condition THEN
Statements
ELSE
Statements
END IF
Example
declare
var1 integer;
begin
var1:=&var1;
if var1>10 and var1<20 then
dbms_output.put_line('Numbe
r is between 10 and 20');
else
dbms_output.put_line('Numbe
r is out of range');
end if;
end;
/
PL/SQL IF-THEN-ELSIF Statement
Syntax:
IF condition THEN
Statements
ELSEIF condition THEN
Statements
ELSE
Statements
END IF
PL/SQL CASE Statement
Syntax:
CASE selector
WHEN 'value1' THEN Statement1;
WHEN 'value2' THEN Statement2;
ELSE Statement
END CASE;
PL/SQL Loops
Syntax:
LOOP
Statement1;
Statement2;
EXIT;
END LOOP;
Example
declare
var1 integer;
begin
var1:=&var1;
loop
dbms_output.put_line(var1);
var1:=var1+1;
exit when var1>5;
end loop;
end;
/
PL/SQL FOR Loop
Syntax:
FOR counter IN var1....var2
LOOP statement1;
statement2;
END LOOP;
Example
declare
var1 integer;
begin
var1:=&var1;
for var1 in 1..5
loop
dbms_output.put_line(va
r1);
end loop;
end;
/
PL/SQL WHILE Loop
Syntax:
WHILE <condition>
LOOP statement1;
statement2;
END LOOP;
Example
declare
var1 integer;
begin
var1:=&var1;
while var1<5
loop
dbms_output.put_line(va
r10);
var1:=var1+1;
end loop;
end;
/