0% found this document useful (0 votes)
7 views19 pages

PL SQL Part 01

Uploaded by

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

PL SQL Part 01

Uploaded by

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

PL/SQL

PL/SQL is Oracle's Procedural Language


extension to SQL.
• PL/SQL has many programming
language features.
• Program units written in PL/SQL can be
stored in compiled form.
• PL/SQL code is portable across all
operating systems that support Oracle.
• PL/SQL does not support DDL and DCL.
PL/SQL Block
A PL/SQL block contains logically related
SQL and PL/SQL statements.
Three sections in a typical PL/SQL block:
• declaration (optional): declare
identifiers (variables and constants).
• execution (required): execute SQL and
PL/SQL statements.
• exception (optional): perform error
handling.
Declaration Section (1)
Examples:
declare
average_GPA number(3,2);
no_of_depts constant number(2) := 23;
no_of_students number(5) not null := 12000;
employee_name varchar2(30);
state_code char(2);
done boolean default true;
• declare one variable at a time!
Declaration Section (2)
PL/SQL Data Types:
binary_integer : -231-1 to 231-1
natural : 0 to 231
positive : 1 to 231
long : variable length character string up to
32,760 bytes (much shorter than the
long type in attribute definition)
boolean : boolean type (true, false, null)
number(n,m), char(n), varchar2(n) , date :
same as their counterparts in SQL
Declaration Section (3)
%type is used to tie the data type of a
PL/SQL variable to an existing data
type or the data type of an attribute of
a relation.
Examples:
v_emp_name employee.name%type;
v_student_gpa Students.gpa%type;
v_course_title Courses.title%type;
• using %type leads to more robust code
Declaration Section (4)
PL/SQL Record Type:
Example:
type course_record_type is record
(course_id Courses.cid%type;
title Courses.title%type;
credit_hours number);
course_record course_record_type;
• use . to reference record fields
course_record.title = ‘Database Systems’;
Declaration Section (5)

%rowtype is used to define a record type


variable and tie it to a table structure.
Example:
one_student Students%rowtype;
• one_student can be used to store a
tuple of Students at a time.
• one_student.name references to the
name attribute.
Execution Section

This section contains executable PL/SQL


statements.
• Null Statement
null;
• Assignment Statement
i := i + 1;
name := ‘Smith’;
Conditional Statement (1)
• Conditional Statements:
IF condition THEN
statement1;
-- one or more statements
END IF;
statement2;
/* one or more statements */
PL/SQL Conditions Involving Null
• is null and is not null are as in SQL
Ex: if (course_title is null) then …
• expressions involving a null value will be
evaluated to a null
Ex: (s1.gpa > s2.gpa) will be evaluated to
null if s1.gpa or s2.gpa is null
• If a condition is evaluated to be false or
null, then the corresponding conditional
statement will not be evaluated.
Truth Table Involving Null

and true false null or true false null


true true false null true true true true
false false false false false true false null
null null false null null true null null

not true false null


false true null
Conditional Statement (2)
A variation of conditional statement:
IF condition THEN
statement1;
ELSE
statement2;
END IF;
statement3;
Conditional Statement (3)
Another conditional statement:
IF condition1 THEN
statement1;
ELSIF condition2 THEN
statement2;
ELSE
statement3;
END IF;
statement4;
Sample Program 1 (1)
declare
student_name Students.Name%TYPE;
student_dept Students.Dept_name%TYPE;
begin
select Name, Dept_name
into student_name, student_dept
/* no : needed before host variables */
from Students where SSN = `123456789';
Sample Program 1 (2)
if (student_dept = ‘Computer Science’) then

dbms_output.put_line(`This is a CS
student.');
else
dbms_output.put_line(`This is not a CS
student.');
end if;
end;
/ /* end each PL/SQL program with / */
Execute PL/SQL Program

• Save the program in a file: sample1.sql


• Execute the program in SQL*Plus
SQL> start sample1
• Enable output to the screen:
SQL> set serveroutput on
or place set serveroutput on at the
beginning of the PL/SQL program.
Loop Statement (1)
Simple loop:
LOOP
statements;
EXIT WHEN condition;
END LOOP;
While loop:
WHILE condition LOOP
statements;
END LOOP;
Sample Program 2
declare
company_payroll number;
begin
select sum(salary)
into company_payroll from Employees;
while company_payroll < 5000000 loop
update Employees
set salary = salary * 1.02;
select sum(salary) into
company_payroll from Employees;
end loop;
end;
/
Loop Statement (2)
For loop:
for variable in [reverse] low_value..high_value
loop
statements;
end loop;
Examples: for x in -10..10
for x in reverse -10..10
• No need to declare loop variable explicitly.
• Loop variable is not accessible outside loop.
• Modifying loop variable will cause an error.

You might also like