PL-SQL Lecture 1
PL-SQL Lecture 1
Introduction to PL/SQL
Programming
What is PL/SQL
• Oracle PL/SQL is an extension of SQL language
• It is designed for seamless processing of SQL statements
enhancing the security, portability, and robustness of the
database.
• Unlike SQL, PL/SQL allows the programmer to write code in a
procedural format.
• Full form of PL/SQL is "Procedural Language extensions to
SQL".
• In simple words, PL/SQL means instructing the compiler 'what
to do' through SQL and 'how to do' through its procedural way.
• Similar to other database languages, it gives more control to
the programmers by the use of loops, conditions and object-
oriented concepts.
PL/SQL Functionalities
• PL/SQL includes procedural language elements like
conditions and loops.
• It allows declaration of constants and variables, procedures
and functions, types and variable of those types and triggers.
• It can support Array and handle exceptions (runtime errors).
• You can create PL/SQL units like procedures, functions,
packages, types and triggers, etc. which are stored in the
database for reuse by applications.
• PL/SQL is not case sensitive so you are free to use lower
case letters or upper case letters except within string and
character literals. A line of PL/SQL text contains groups of
characters known as lexical units.
PL/SQL Block Structure
• PL/SQL program units organize the code into blocks.
• A block without a name is known as an anonymous
block.
• The anonymous block is the simplest unit in PL/SQL.
• It is called anonymous block because it is not saved in
the Oracle database.
• This is the component which has the actual PL/SQL
code.
• This consists of different sections to divide the code
logically (declarative section for declaring purpose,
execution section for processing statements, exception
handling section for handling errors)
The following illustrates anonymous block syntax:
[DECLARE] --optional
Declaration statements;
Execution --mandatory
BEGIN
Execution statements;
[EXCEPTION] --optional
Exception handling statements;
END;
/
• The declaration section allows you to define data types,
structures, and variables.
• The execution section is required in a block structure and it
must have at least one statement. 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. It should have at least one executable line of
code, which may be just a NULL command to indicate that
nothing should be executed.
• The exception handling section is starting with
the EXCEPTION keyword. The exception section is the place
that you put the code to handle exceptions
• Notice that the single forward slash (/) is a signal to instruct
SQL*Plus to execute the PL/SQL block.
The Declaration Section
• Is an optional section.
• Used to declare the variables that will be used in the program.
• A variable is an object that can hold a value in a program.
• A variable has a name and datatype .
• Every variable referenced in the program must be declared in
the declaration section.
• Example:
Begin
Dbms_output.put_line(‘Hello-world’);
End;
/
Declare
Ename varchar2(10) :=‘CMRIT’;
Begin
Dbms_output.put_line(‘hello’ || ename);
End;
/
Examples
Bday date := ‘10-june-98’;
Salary number(10,2):= 5500;
Ename varchar2(15):=‘Sidharth’;
Active boolean:= false;
Declaring a variable as BEGIN
constant
-- processing
radius := 9.5;
constant_name CONSTANT
datatype := VALUE; dia := radius * 2;
Example circumference := 2.0 * pi * rad
ius;
area := pi * radius * radius;
DECLARE
-- output
-- constant declaration
dbms_output.put_line('Radius
pi constant number := 3.141 : ' || radius);
592654;
dbms_output.put_line('Diame
-- other declarations ter: ' || dia);
radius number(5,2); dbms_output.put_line('Circu
dia number(5,2); mference: ' || circumference);
circumference number(7, 2); dbms_output.put_line('Area: '
|| area);
area number (10, 2); END;
/
The Body of the PL/SQL Block
• Every PL/SQL block should have a body
• The body is contained of one or more executable statements
enclosed between keywords BEGIN and END.
• Each executable statement is terminated with a semicolon.
• The termination of the block is indicated by a forward slash as the first
character on a new line.
DBMS_OUTPUT.PUT_LINE
• Used to display a string on the screen.
• The string should be enclosed in single quotes.
• A variable name should not be enclosed in single quotes – the
values contained in the variable will be displayed on the
screen.
• If the variable is not a character variable, use the to_char
function around the variable name.
• Combine a string with a variable with the use of the
concatenation character(double pipe ||)
SELECT statement in PL/SQL
select column INTO mem_variable
from table_name
[where condition and other clauses]
Example
Select column1,column2,… into mem_variable1,mem_variable2,..
From table_name where condition…
Declare
Vsal number(10,2);
Begin
Select salary into vsal from employees
Where employee_id=102;
DBMS_OUTPUT.PUT_LINE(‘The salary earned by employee is :’ ||
to_char(vsal));
End;
/
Declare
Dept_no number(5);
Ename varchar2(15);
Vsal number(10,2);
Begin
Select salary,department_id,name into vsal,dept,ename
from employees
Where employee_id=102;
DBMS_OUTPUT.PUT_LINE(‘The name of the employee
is : ‘ || ename);
DBMS_OUTPUT.PUT_LINE(ename || ‘works in the ‘
||to_char(dept) || ‘department);
DBMS_OUTPUT.PUT_LINE(‘The salary earned by
employee is :’ ||to_char(vsal));
End;
/
Simple IF Statement
Syntax: (IF-THEN statement):
IF condition
THEN
Statement: {It is executed when condition is true}
END IF;
This syntax is used when you want to execute statements only
when condition is TRUE.
Example
Declare
Vsal number(10,2);
Begin
Select salary into vsal from employees
Where first_name=‘prashant’;
If vsal > 7000 then
DBMS_OUTPUT.PUT_LINE(‘Earns more than Anand’);
End if;
End
/
Syntax: (IF-THEN-ELSE statement):
IF condition
THEN
{...statements to execute when condition is TRUE...}
ELSE
{...statements to execute when condition is FALSE...}
END IF;
It is the most advance syntax and used if you want to execute one
set of statements when condition1 is TRUE, a different set of
statement when condition2 is TRUE or a different set of statements
when both the condition1 and condition2 are FALSE.
Declare
Vsal number(10,2);
Begin
Select sal into vsal from emp
Where ename=‘prashant’;
If vsal > 9000 then
DBMS_OUTPUT.PUT_LINE(‘Earns more than Rohan’);
elseif vsal > 7000 then
DBMS_OUTPUT.PUT_LINE(‘Earns less than Anand’);
elseif vsal > 4000 then
DBMS_OUTPUT.PUT_LINE(‘Earns less than Suresh’);
Else
DBMS_OUTPUT.PUT_LINE(‘Earns less than everyone
else’);
End if;
End
/
PL/SQL CASE statement
The CASE statement works like the IF statement, only using the
keyword WHEN. A CASE statement is evaluated from top to
bottom. If it get the condition TRUE, then the corresponding THEN
clause is executed and the execution goes to the END CASE clause.
Syntax
CASE [ expression ]
WHEN condition_1 THEN result_1
WHEN condition_2 THEN result_2
...
WHEN condition_n THEN result_n
ELSE result
END
DECLARE
grade char(1) := 'A';
BEGIN
CASE grade
when 'A' then dbms_output.put_line('Excellent');
when 'B' then dbms_output.put_line('Very good');
when 'C' then dbms_output.put_line('Good');
when 'D' then dbms_output.put_line('Average');
when 'F' then dbms_output.put_line('Passed with Grace');
else dbms_output.put_line('Failed');
END CASE;
END;
/
Iterative Control
• Iterative statements are used to execute a particular
statement or block of statements several times in a
program.
• It facilitates the execution of same block of statement a
number of times until a condition is satisfied
• We use the loop constructs in a program, to repeat an
certain action multiple times in the program
• There are three types of LOOP statements
– LOOP….. END LOOP
– WHILE(CONDITION) LOOP…. END LOOP
– FOR …. LOOP END LOOP
Syntax for a basic loop:
LOOP
Sequence of statements;
END LOOP;
LOOP
statements;
EXIT;
{or EXIT WHEN condition;}
END LOOP;
DECLARE
DECLARE VAR1 NUMBER;
i NUMBER := 1; VAR2 NUMBER;
BEGIN BEGIN
LOOP VAR1:=100;
EXIT WHEN i>10; VAR2:=1;
DBMS_OUTPUT.PUT_LINE(i) LOOP
; DBMS_OUTPUT.PUT_LINE
i := i+1; (VAR1*VAR2);
END LOOP; IF (VAR2=10) THEN
END; EXIT;
END IF;
VAR2:=VAR2+1;
END LOOP;
END;
PL/SQL While Loop
PL/SQL while loop is used when a set of statements has to be
executed as long as a condition is true, the While loop is used
WHILE <condition>
LOOP statements; DECLARE
VAR1 NUMBER;
END LOOP; VAR2 NUMBER;
Example BEGIN
DECLARE VAR1:=200;
i INTEGER := 1; VAR2:=1;
WHILE (VAR2<=10)
BEGIN
LOOP
WHILE i <= 10 LOOP DBMS_OUTPUT.PUT_LINE (VAR1
DBMS_OUTPUT.PUT_LINE(i); *VAR2);
i := i+1; VAR2:=VAR2+1;
END LOOP; END LOOP;
END;
END;
PL/SQL FOR Loop
‘PL/SQL for loop’ is used when you want to execute a set of
statements for a predetermined number of times. The loop is
iterated between the start and end integer values. The
counter is always incremented by 1 and once the counter
reaches the value of end integer, the loop ends.
FOR counter IN initial_value .. final_value LOOP
LOOP statements;
END LOOP;
Example
BEGIN
FOR k IN 1..10 LOOP
-- note that k was not declared
DBMS_OUTPUT.PUT_LINE(k);
END LOOP;
END;
• You don't need to declare the counter variable explicitly
because it is declared implicitly in the declaration
section.
• The counter variable is incremented by 1 and does not
need to be incremented explicitly.
• You can use EXIT WHEN statements and EXIT
statements in FOR Loops but it is not done often.
DECLARE
VAR1 NUMBER;
BEGIN
VAR1:=10;
FOR VAR2 IN 1..10
LOOP
DBMS_OUTPUT.PUT_LINE (VAR1*VAR2);
END LOOP;
END;