UNIT5 - PLSQL Introduction 1
UNIT5 - PLSQL Introduction 1
Introduction
PL/SQL stands for Procedural Language/Structured Query
language.
The PL/SQL programming language was developed by
Oracle Corporation in the late 1980s as procedural
extension language for SQL.
It is the superset of the SQL.
Because it is procedural language it removes many
restrictions of SQL language.
Disadvantage of SQL
SQL don’t have procedural capabilities like condition
checking, looping, and branching.
SQL can only manipulate the information stored into
database.
Checks if the value of left operand is greater than the value of right
> (A > B) is not true.
operand, if yes then condition becomes true.
Checks if the value of left operand is less than the value of right
< (A < B) is true.
operand, if yes then condition becomes true.
Checks if the value of left operand is greater than or equal to the value
>= (A >= B) is not true.
of right operand, if yes then condition becomes true.
Checks if the value of left operand is less than or equal to the value of
<= (A <= B) is true.
right operand, if yes then condition becomes true.
Additional Comparison Operators
The LIKE operator compares a character, string, or If 'Zara Ali' like 'Z% A_i' returns a
LIKE CLOB value to a pattern and returns TRUE if the Boolean true, whereas, 'Nuha Ali'
value matches the pattern and FALSE if it does not. like 'Z% A_i' returns a Boolean false.
Operat
Description Example
or
Character
'A' '%' '9' ' ' 'z' '('
Literals
'Hello, world!'
String Literals
'19-NOV-12'
BOOLEAN
TRUE, FALSE, and NULL.
Literals
DECLARE
a number := 10;
b number := 20;
c number;
Declaring a Constant
A constant variable cannot be changed throughout the
program.
DECLARE
PI CONSTANT NUMBER := 3.14 -- constant declaration
Data types
Number- integers and floating point number.
Char: alphanumeric up to 32767 bytes
Varchar: variable length alphanumeric
Date: date and time
Boolean: true, false or null
Declaration
Variable declaration
variable-name datatype(size);
Constant declaration
variable-name CONSTANT datatype(size) := value;
Assignment
1. Using assignment operator (:=)
A :=10;
Sum := A+B+C;
:=
is not the same as the equality operator
=
All statements end with a ;
PL/SQL Comments
Single line comment
A:=5; -- assign value 5 to variable A.
Multi-line comments
A:=b+c; /* the value of variable A and B are added and
assign to variable A */
Important PL/SQL delimiters
+, -, *, / arithmetic operators
; statement terminator
:= assignment operator
=> association operator
|| strings concatenation operator
. component indicator
% attribute operator
‘ character string delimiter
-- single line comment
/*, */ multi line comment delimiters
.. range operator
=, >, >=, <, <= relational operators
!=, ~=, ^=, <> not equal relational operators
is null, like, between PL/SQL relational operators
To display user message on the screen
SQL> Set Serveroutput ON;
dbms_output.put_line(A);
dbms_output.put_line(‘Value of A is:’ || A);
|| is concatenation operator
Read a value during runtime
Num:= :num;
A) Subset
B) Superset
C) Powerset
D) None of these
Quiz
A PL/SQL statement is terminated with
A) End statement
B) Stop Statement
C) Break Statement
D) None of these
Quiz
Which of the following is the assignment operator in
Oracle
A) =
B) :=
C) ==
D) None of these
To add two numbers
Declare
a number(2);
b number (2);
c number(2);
Begin
a:=5;
b:=2;
c:=a+b;
dbms_output.put_line(‘sum=‘ || c);
End;
To add two numbers (get values from user)
Declare
a number(2);
b number (2);
c number(2);
Begin
a:=:a;
b:=:b;
c:=a+b;
dbms_output.put_line(‘sum=‘ || c);
End;
Create a ‘sty’ table with following parameters:
Select Marks and name from ‘sty’ table and also update ‘sty’ table
declare
a int;
n varchar(20);
begin
select marks,name into a,n from sty where id=3;
dbms_output.put_line(a ||' '||n);
update sty set marks=a+7 where id=3;
dbms_output.put_line(a);
end;
emp table
Emp_name Emp_id TA DA Total Branch_City
abc 10 1200 1345 2545 Delhi
xyz 12 1100 1200 2300 Mumbai
calculate total amount(ta+da) of an
employee, also update the emp table
PL/SQL code to calculate total amount(ta+da)
of an employee, also update the emp table
Declare
a number(5);
b number(5);
t number(5);
Begin
select ta, da into a, b from emp where empid=12;
t:=a+b;
update emp set total =t where empid=12;
end;
Variable attributes
%type
%rowtype
%TYPE
Provide the data type of a variable or column.
Exp:
sal employee.salary%TYPE;
declare
a sty.marks%type;
n sty.name%type;
begin
select marks,name into a,n from sty where id=2;
dbms_output.put_line(a ||' '||n);
end;
Declare
a emp.ta%TYPE;
b emp.td%TYPE;
t emp.total%TYPE;
Begin
Select ta, da into a, b from emp where emp_id=12;
t=a+d;
Update emp set total =t where empid=12;
End;
%ROWTYPE
It provides a record type that represents a row in a table. One
variable to access the complete row of the table.
Eg:
dept_rec dept%ROWTYPE; -- declaring record variable.
detp_rec.deptno;
dept_rec.deptname; -- accessing columns
i.e. recordname.colname
record2.total=record2.ta+record2.da;
IF condition then
Sequence of statements;
Else
Sequence of statements;
End if;
IF condition1 then
Sequence of statements;
Elsif condition2 then
Sequence of statements;
Else
Sequence of statements;
End if;
To find largest of two numbers
Declare
num1 number(2);
num2 number(2);
Begin
num1 := :num1;
num2 := :num2;
If num1>num2 then
dbms_output.put_line(‘greater number is =‘ || num1);
Else
dbms_output.put_line(‘greater number is =‘ || num2);
End if
End;
Even or odd number
declare
n number:=:n;
begin
if mod(n,2)=0
then
dbms_output.put_line('number is even');
else
dbms_output.put_line('number is odd');
end if;
end;
To find largest of three numbers(nested if)
To display the grade of students according to marks(elsif).
To find largest of three numbers(nested if)
declare
a number:=10;
b number:=12;
c number:=5;
begin
dbms_output.put_line('a='||a||' b='||b||' c='||c);
if a>b AND a>c
then
dbms_output.put_line('a is greatest');
else
if b>a AND b>c
then
dbms_output.put_line('b is greatest');
else
dbms_output.put_line('c is greatest');
end if;
end if;
end;
QUIZ
B - It is a mandatory section.
While condition
Loop
Sequence of statements;
updation;
End loop;
To print Square of number from 1 to 10
Declare
a number(2);
Begin
a:=1;
While a<=10;
Loop
Dbms_output.put_line(a*a);
a:=a+1;
End loop;
End;
To print multiplication table
Declare
table number := &table;
count number:=1;
result number;
Begin
While count<=10
Loop
result := table*count;
Dbms_output.put_line (table|| ‘*’ || count ||‘=’|| result);
count:=count+1;
End loop;
End;
For loop
A. GOTO
B. EXIT WHEN
C. CONTINUE WHEN
D. KILL
Quiz
Goto lablename;
Eg
Drop trigger t11
Cursors
A cursor is a work area where the result of a SQL query is
stored at server side.
A cursor is a PL/SQL construct that allows us to name
these work area.
The data stored in the cursor is known as active data set
Declare a cursor
Open a cursor
Fetch or Read from a cursor
Close cursor
Types of cursors
Implicit cursor
It is a work area that is declared, opened and closed internally
by the oracle engine. PL/SQL declared a cursor implicitly for
all SQL data manipulation statements.
Explicit cursor ( user defined)
It is a work area that is declared, opened and closed externally
by the user.
Define in DECLARE section of PL/SQL block
General Cursor attributes
To keep status of a cursor:
%ISOPEN
%FOUND
%NOTFOUND
%ROWCOUNT
Steps of execution
Declare the cursor
Open the cursor
Using loop, fetch the data from cursor one row at a time
and store in memory variable
Exit from the loop
Close the cursor
Declaring a cursor
Cursor cursorname IS select statements.
Loop
FETCH C123 into my_record;
Exit when C123%notfound;
--Other statements;
End loop;
Closing a cursor
CLOSE cursorname;
CLOSE C123;
Write a PL/SQL cursor to display the name
of the students belonging to CSE branch
Declare
Cursor C123 is select name from customers where address ='Delhi';
my_name customers.name%type;
Begin
Open C123;
Loop
Fetch C123 into my_name;
Exit when C123%NotFound;
dbms_output.put_line(my_name);
End loop;
Close C123;
End;
PL/SQL subprograms are named PL/SQL blocks that
can be invoked with a set of parameters. PL/SQL
provides two kinds of subprograms −
Functions − These subprograms return a single value;
mainly used to compute and return a value.
Procedures − These subprograms do not return a value
directly; mainly used to perform an action.
Creating a Procedure
A procedure is created with the CREATE OR
REPLACE PROCEDURE statement. The simplified
syntax for the CREATE OR REPLACE
PROCEDURE statement is as follows −
CREATE [OR REPLACE] PROCEDURE procedure_name
[(parameter_name [IN | OUT | IN OUT] type [, ...])]
{IS | AS}
BEGIN
< procedure_body >
END procedure_name;
Where,
procedure-name specifies the name of the procedure.
[OR REPLACE] option allows the modification of an existing procedure.
The optional parameter list contains name, mode and types of the parameters.
IN represents the value that will be passed from outside and OUT represents the
parameter that will be used to return a value outside of the procedure.
procedure-body contains the executable part.
The AS keyword is used instead of the IS keyword for creating a standalone procedure.
Executing a Standalone Procedure
A standalone procedure can be called in two ways −
•Using the EXECUTE keyword
•Calling the name of the procedure from a PL/SQL block
BEGIN
num:= 6;
factorial := fact(num);
dbms_output.put_line(' Factorial '|| num || ' is ' || factorial);
END;