Database Systems: Lab 07 PL SQL
Database Systems: Lab 07 PL SQL
Lab 07
PL SQL
PL/SQL
Oracle added a procedural programming language known as
PL/SQL to the database
• It contains the standard programming constructs you would
expect from such a language, such as:
• Block Structure
• Variable & types
• Conditional Logic
• Loops
• Cursors, which holds the results returned by a query
• Procedures
• Functions
• Packages, which may be used to group procedures and functions
together in one unit
Block Structures
• PL/SQL programs are divided into structures known as blocks,
with each block containing PL/SQL and SQL statements
• DECLARE (optional)
• Variables, cursors, user-defined exceptions
• BEGIN (mandatory)
• SQL statements
• PL/SQL statements
• EXCEPTION (optional)
• Actions to perform
when errors occur
• END; (mandatory)
Variables and Types
Variable declared within the DECLARE section may only be referenced within that block. A
variable declaration has both a name and a type.
• Syntax:
[Name of the variable] [Type];
• Example:
The following example illustrates some more variable declarations that may be used to store
the column values from the EMP Table:
Empno Number(14);
Ename Varchar2(14);
Job Varchar2(14);
Deptno Number(7,2);
Sal Number(20);
sales number(10, 2);
pi CONSTANT double precision := 3.1415;
counter binary_integer := 0;
greetings varchar2(20) DEFAULT 'Have a Good Day';
• DECLARE
• Width INTEGER;
• Height INTEGER := 2 ;
• Area INTEGER;
• BEGIN
• Area := 6;
• Width := area/height;
• DBMS_OUTPUT.PUT_LINE( 'width = ' || width);
• EXCEPTION
• WHEN ZERO_DIVIDE THEN
• DBMS_OUTPUT.PUT_LINE('Division by Zero');
• END;
• /
Program 2
• declare
• eno emp.empno%type;
• name emp.ename%type;
• begin
• eno := 2;
• select ename into name from emp where
• empno=eno;
• dbms_output.put_line('name of the employee is '||name);
• end;
• /
Program 3
• declare
• eno emp.empno%type;
• name emp.ename%type;
• begin
• eno := 2;
• select ename into name from emp where
• empno=eno;
• dbms_output.put_line('name of the employee is '||name);
• EXCEPTION
• WHEN NO_DATA_FOUND
• THEN dbms_output.put_line('No Data Found');
• end;
• /
Program 4 – Taking input
• SET SERVEROUTPUT ON
• declare
• eno emp.empno%type;
• name emp.ename%type;
• begin
• eno := &eno;
• select ename into name from emp where
• empno=eno;
• dbms_output.put_line('name of the employee is '||name);
• EXCEPTION
• WHEN NO_DATA_FOUND
• THEN dbms_output.put_line('No Data Found');
• end;
• /
Conditional Logic
• Syntax:
IF condition1 THEN
statements1
ELSEIF condition2 THEN
statements2
ELSE
statements3
END IF;
Variable Scope in PL/SQL
• DECLARE
• num1 number := 95;
• num2 number := 85;
• BEGIN
• dbms_output.put_line('Outer Variable num1: ' || num1);
dbms_output.put_line('Outer Variable num2: ' || num2);
• DECLARE
• num1 number := 195;
• num2 number := 185;
• BEGIN
• dbms_output.put_line('Inner Variable num1: ' || num1);
dbms_output.put_line('Inner Variable num2: ' || num2);
• END;
• END;
• /
Nested IF Statement
IF count > 0 THEN
Message := ‘count is positive’;
IF area > 0 THEN
Message := ‘count and area are positive’;
END IF;
ELSEIF count = 0 THEN
Message := ‘count is zero’;
ELSE
Message := ‘count is negative’;
END IF;
IF-THEN-ELSIF
• DECLARE
• a number(3) := 100;
• BEGIN
• IF ( a = 10 ) THEN
• dbms_output.put_line('Value of a is 10' );
• ELSIF ( a = 20 ) THEN
• dbms_output.put_line('Value of a is 20' );
• ELSIF ( a = 30 ) THEN
• dbms_output.put_line('Value of a is 30' );
• ELSE
• dbms_output.put_line('None of the values is matching');
• END IF;
• dbms_output.put_line('Exact value of a is: '|| a );
• END;
• /
ITERATIONS IN PL/SQL
• Sequence of statements can be executed any number of times
using loop construct.
• Simple Loop
• For Loop
• While Loop
SIMPLE LOOP
• Syntax:
• LOOP
• statement1;
• EXIT [ WHEN Condition];
• END LOOP;
Program 5
• SET SERVEROUTPUT ON;
• Declare
• x number:=10;
• Begin
• LOOP
• dbms_output.put_line(x);
• x := x + 10;
• IF x > 50 THEN
• exit;
• END IF;
• END LOOP;
• dbms_output.put_line(to_char(x));
• end;
• /
WHILE LOOP
• Syntax
• WHILE condition LOOP
• statement1;
• statement2;
• END LOOP;
Program 6
• Declare
• i number:=0;
• j number:=0;
• begin
• While i<=100 Loop
• j := j+i;
• i := i+2;
• end loop;
• dbms_output.put_line(‘the value of j is’ ||j);
• end;
• /
FOR LOOP
• Syntax:
• FOR counter IN [REVERSE]
• LowerBound..UpperBound
• LOOP
• statement1;
• statement2;
• END LOOP;
Program 7
• Example:
• SET SERVEROUTPUT ON;
• DECLARE
• a number(2);
• BEGIN
• FOR a in 10 .. 20 LOOP
• dbms_output.put_line('value of a: ' || a);
• END LOOP;
• END;
• /
Array
• Creating a Varray Type for Array.
• Syntax:
• TYPE varray_type_name IS VARRAY(n) of <element_type>