Lab Manual
Lab Manual
Lab Manual
1. Declarations
This section starts with the keyword DECLARE. It is an optional section
and defines all variables, cursors, subprograms, and other elements to be
used in the program.
Con…
2. Executable Commands
This section is enclosed between the keywords BEGIN and
END and it is a mandatory section.
It consists of the executable PL/SQL statements of the program.
a) Scalar – this data types hold a single value. (data types that
corresponds with column types.
b) Composite – they allow groups of fields to be defined and
manipulated in blocks.
c) References – they hold values, called pointers, but designate
other program items.
PL/SQL Programming
set serveroutput on
Declare
a integer;
b integer;
c integer;
f real;
Begin
a:=&a;
b:=&b;
c:=a+b;
f:=a/b;
dbms_output.put_line('a+b = '||c);
dbms_output.put_line('a/b = '||f);
end;
/
create table circle(
radius number(5,2),
diameter number(5,2),
circum number(5,2),
area number(5,2)
);
declare
pi constant number:=3.14;
rad circle.radius%type:=4.5;
dia circle.diameter%type;
cirm circle.circum%type;
area circle.area%type;
begin
dia := 2* rad;
cirm:=2.0*pi*rad;
area:=pi*rad**2;
insert into circle values(rad, dia, cirm, area);
end;
/
Variable Scope in PL/SQL
IF-THEN Statement
IF condition THEN
S;
END IF;
IF-THEN-ELSE Statement
IF condition THEN
S1;
ELSE
S2;
END IF;
Con…
IF-THEN-ELSIF Statement
IF(boolean_expression 1)THEN
ELSE
END IF;
Example
create table emp(
eno integer,
ename varchar(12),
ebdate date,
esalary number(9,2)
);
insert into emp values(1,'Dawit','12-sep-1990',4500);
insert into emp values(2,'Beti','22-Jan-1990',6500);
insert into emp values(3,'Chala','21-feb-1990',7500);
insert into emp values(4,'Sara','19-jul-1990',8500);
insert into emp values(5,'Dani','16-apr-1990',9500);
declare
id emp.eno%type:=&id;
name emp.ename%type;
sal emp.esalary%type;
begin
select eno, ename, esalary into id,name,sal from emp where eno=id;
if(sal<6000) then
dbms_output.put_line('Employee '||name||' earns the min salry of '||sal);
end if;
end;
/
CASE Statement
Example
Searched CASE Statement
PL/SQL — Loops
For loop
While Loop
Basic Loop Statement
WHILE LOOP Statement
FOR LOOP Statement
Syntax
Example
DECLARE
a
number(2);
BEGIN
FOR a
in 10 ..
20 LOOP
dbms_output.put_line('value
of a: ' || a); END LOOP;
END;
Reverse FOR LOOP Statement
The Loop Control Statements
Example
DECLARE
a number(2) :=10;
BEGIN
-- while loop execution
WHILE a < 20 LOOP
dbms_output.put_line ('value of a: ' || a);
a := a + 1;
IF a > 15 THEN
-- terminate the loop using the exit statement
EXIT;
END IF;
END LOOP;
END;
/
Continue statement
Example
DECLARE
a number(2) := 10;
BEGIN
-- while loop execution
WHILE a < 20 LOOP
dbms_output.put_line ('value of a: ' || a);
a := a + 1;
IF a = 15 THEN
-- skip the loop using the CONTINUE statement a
:= a + 1;
CONTINUE
; END IF;
END LOOP;
END;
/
Goto statement
DECLARE
a number(2) := 10; BEGIN
<<loopstart>>
-- while loop execution
WHILE a < 20 LOOP
dbms_output.put_line ('value of a:
' || a);
a := a + 1;
IF a = 15 THEN
a := a + 1;
GOTO loopstart;
END IF;
END LOOP;
END;
/
SQL Methods
Procedure
Function
Triggers
Triggers are stored programs, which are automatically executed or fired when
some events occur.
Triggers are, in fact, written to be executed in response to any of the following
events:
A database manipulation DML statement DELETE, INSERT, or UPDATE.
A database definition DDL statement CREATE, ALTER, or DROP.
Syntax
User-defined Exceptions