Professional Documents
Culture Documents
PLSQL Introduction (Unit6)
PLSQL Introduction (Unit6)
PLSQL Introduction (Unit6)
Created by :
Amanpreet Kaur
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 'Tutorials Point'
'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:= #
Exp:
sal employee.salary%TYPE;
%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;
To find largest of three numbers(nested if)
To display the grade of students according to marks(elsif).
Iterative control
Sequence of statements can be executed number of times
Loop
While – loop
For-loop
Loop
Simple loop (Infinite loop)
Loop
Sequence of statements;
Exit when condition;
End loop;
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
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 student where branch
=‘CSE’;
my_name student.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;