PLSQL Half
PLSQL Half
PLSQL Half
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.
Operat
Description Example(A=10, B=5)
or
!=
Checks if the value of two operands is equal or not, if
<> (A != B) is true.
values are not equal then condition becomes true.
~=
Operato
Description Example
r
The LIKE operator compares a character, If 'Zara Ali' like 'Z% A_i'
string, or CLOB value to a pattern and returns a Boolean true,
LIKE
returns TRUE if the value matches the whereas, 'Nuha Ali' like 'Z%
pattern and FALSE if it does not. A_i' returns a Boolean false.
If x = 10 then, x between 5
The BETWEEN operator tests whether a and 20 returns true, x
BETWEE
value lies in a specified range. x BETWEEN between 5 and 10 returns
N
a AND b means that x >= a and x <= b. true, but x between 11 and 20
returns false.
Opera
Description Example
tor
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