0% found this document useful (0 votes)
79 views22 pages

Database Systems: Lab 07 PL SQL

1. PL/SQL is a procedural language extension for SQL and Oracle databases that allows for block structuring and variable declarations. It contains standard programming constructs like conditionals, loops, and procedures. 2. PL/SQL code is organized into optional declare, mandatory begin, and optional exception blocks. Variables can be declared and referenced within blocks. 3. Common programming constructs like conditionals, loops, and arrays can be implemented in PL/SQL to perform operations on database objects and values. User input can also be taken to customize program behavior.

Uploaded by

Neha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views22 pages

Database Systems: Lab 07 PL SQL

1. PL/SQL is a procedural language extension for SQL and Oracle databases that allows for block structuring and variable declarations. It contains standard programming constructs like conditionals, loops, and procedures. 2. PL/SQL code is organized into optional declare, mandatory begin, and optional exception blocks. Variables can be declared and referenced within blocks. 3. Common programming constructs like conditionals, loops, and arrays can be implemented in PL/SQL to perform operations on database objects and values. User input can also be taken to customize program behavior.

Uploaded by

Neha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Database Systems

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';

• USING %TYPE keyword:


It is used to declare a variable of the same type as a specified column in a table:
SAL EMP.SAL%TYPE;
Program 1
• SET SERVEROUTPUT ON;

• 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.

• It is broadly classified into:

• 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>

• Use the type in array.

• Identifier := varray_type_name (n values);


Array Example:
• DECLARE
• type namesarray IS VARRAY(5) OF VARCHAR2(10);
• type grades IS VARRAY(5) OF INTEGER;
• names namesarray;
• marks grades;
• total integer;
• BEGIN
• names := namesarray(‘Areeba', ‘Asma', 'Ali', ‘Hibba', ‘Maliha');
• marks:= grades(98, 97, 78, 87, 92);
• total := names.count;
• dbms_output.put_line('Total '|| total || ' Students');
• FOR i in 1 .. total LOOP
• dbms_output.put_line('Student: ' || names(i) || '
• Marks: ' || marks(i));
• END LOOP;
• END;
• /
Exercise
• Write a PL/SQL code that takes two inputs from user, add them and
store the sum in new variable and show the output.
• Write a PL/SQL code that takes two inputs, lower boundary and
upper boundary, then print the sum of all the numbers between
the boundaries INCLUSIVE.
• Write a PL/SQL code to retrieve the employee name, hiredate, and
the department name in which he works, whose number is input
by the user.
• Write a PL/SQL code to check whether the given number is
palindrome or not.
• Write a PL/SQL code that takes all the required inputs from the
user for the Employee table and then insert it into the Employee
and Department table in the database.
• Write a PL/SQL code to find the first employee who has a salary
over $2500 and is higher in the chain of command than employee
7499. Note: For chain, use of LOOP is necessary.
• Write a PL/SQL code to print the sum of first 100 numbers.

You might also like