Академический Документы
Профессиональный Документы
Культура Документы
-------
Features:
---------
-> it executes a block of statements as a unit.
-> it supports control structures and iteration control statements.
-> it supports exception handling
-> pl/sql supports automatic execution of code, through db triggers
-> pl/sql supports sub-programs(procedures and functions)
-> pl/sql supports packages.
-> it supports to share the programs from one user to another user and it supports diffrerent appl.
software tools.
Block:
------
-> it is a collection of sql and non-sql statements.
-> there are two types of blocks.
1. Anonymous Block
2. Named Block
1. Anonymous Block:
------------------
-> this block of statements are not stored in db permanently,
it is stored in SQL BUFFER.
SQL BUFFER: it is a temporary buffer, and it holds last executed query.
-> to save anonymous block programs, user can explicitely save it by using save command.
2. Named Block:
---------------
-> this block of statements are stored in db permanently.
ex: Triggers, Procedures, Functions & Packages
Simple Block:
------------
Begin
<exec-statements>;
End;
pl/sql supports DML,DRL & TCL Commands.
pl/sql not supported DDL & DCL Commands.
BOOLEAN:
-------
-> this datatype stored either TRUE or FALSE.
ex: Flag Boolean:=TRUE/FALSE;
:=
---
-> it is an assignment operator, used to assign the values into given variable.
ex: a:=10; (10 value is assigned to 'a' variable)
comments:
---------
-> comments are description of the given statement.
-> comments are non-executable statements.
-> there are two types of comments.
1. single line comment
2. Multi line comment
1.single comment:
----------------
-> comment contains single line is called as single line comment.
-> single line comments are represented with '--'
EX: -- pl/sql block for to display welcome message
/* - - - - - -
- - - - - - - */
DBMS_OUTPUT.PUT_LINE(Arg1):
--------------------------
-> it is one of the output statement.
-> it returns output to sql buffer
-> it accepts only one argument at a time.
ex: dbms_output.put_line('welcome to pl/sql');
example:
Begin
dbms_output.put_line('welcome to World');
End;
Declare
a number(2):=10;
b number(2):=20;
Begin
dbms_output.put_line((a+b));
End;
Declare
a number(2):=&a;
b number(2):=&b;
Begin
dbms_output.put_line((a+b));
End;
Declare
a number(2):=&a;
b number(2):=&b;
Begin
dbms_output.put_line('Sum of Two Numbers = '||(a+b));
End;
-> write a pl/sql block to accept employee number and display corresponding employee details.
Begin
SELECT EMPNO,ENAME,SAL,DEPTNO FROM EMP WHERE EMPNO=7788;
End;
select..into:
-------------
-> used to copies database column vlaues into pl/sql variables.
-> no.of db columns must be matched with no.of pl/sql variables and corresponding datatype should be
matched.
syntax:
-------
select columm1,column2,... into variable1, variable2,... from <table_name>
where <condition>;
ex:
---
select empno,ename,sal,deptno into tempno,tename,tsal,tdeptno from emp
where empno=7788;
example:
--------
Declare
tempno number(4);
tename varchar2(10);
tsal number(8,2);
tdeptno number(2);
Begin
select empno,ename,sal,deptno into tempno,tename,tsal,tdeptno from emp
where empno=&empno;
dbms_output.put_line(tempno||' '||tename||' '||tsal||' '||tdeptno);
End;
Declare
tempno number(4);
tename varchar2(10);
tsal number(7,2);
tdeptno number(2);
Begin
select empno,ename,sal,deptno into tempno,tename,tsal,tdeptno from emp
where empno=&empno;
dbms_output.put_line(tempno||' '||tename||' '||tsal||' '||tdeptno);
End;
Attributes:
----------
-> used to declare variable datatype dynamically according to specified structure.
-> basically there are two types of attributes.
i. column type attribute
ii. rowtype attribute
i.column type attribute:
-----------------------
-> used to declare variable datatypes dynamically according to specified table of column structure.
syntax:
-------
<variable_name> <table_name>.<column_name>
ex:
Tempno Emp.Empno%type;
Tsal Emp.Sal%type;
Example:
--------
Declare
tempno emp.empno%type;
tename emp.ename%type;
tsal emp.sal%type;
tdeptno emp.deptno%type;
Begin
select empno,ename,sal,deptno into tempno,tename,tsal,tdeptno from emp
where empno=&empno;
dbms_output.put_line(tempno||' '||tename||' '||tsal||' '||tdeptno);
End;
syntax:
-------
<variable_name> <table_name>%rowtype;
ex:
Erec Emp%rowtype;
Example:
--------
Declare
E Emp%rowtype;
Begin
Select * into E from Emp where empno=&empno;
dbms_output.put_line(e.empno||' '||e.ename||' '||e.job||' '||e.hiredate||' '||e.sal||' '||e.deptno);
End;
-- write a pl/sql block to display netsalary for a given employee number.
Declare
Tempno Emp.Empno%type:=&Tempno;
Tsal Emp.Sal%type;
Tcomm Emp.Comm%type;
Netsal Number;
Begin
select Sal,Comm into Tsal,Tcomm From Emp
where Empno=Tempno;
Netsal:=Tsal+Nvl(Tcomm,0);
Dbms_output.put_line('Employee Number : '||Tempno);
Dbms_output.put_line('Employee Basic Salary : '||TSal);
Dbms_output.put_line('Employee Commission : '||Nvl(Tcomm,0));
Dbms_output.put_line('Employee Net Salar : '||Netsal);
End;
Control Structures:
-------------------
-> used to control flow of the program.
-> there are three types of control structures.
ii. if..else:
------------
if <condition> then
<exec-statements>; -- true block
else
<exec-statements>; -- false block
end if;
example:
--------
-- write a pl/sql block to display given no. is even or odd.
Declare
Num Number(3):=&Num;
Begin
if Mod(Num,2)=0 then
dbms_output.put_line('Given No. is Even.');
else
dbms_output.put_line('Given No. is Odd.');
end if;
End;
syntax:
-------
if <condition> then
if <condition> then
<exec-statement>;
else
<exec-statements>;
end if;
else
if <condition> then
<exec-statement>;
else
<exec-statements>;
end if;
end if;
example:
--------
-- pl/sql block for to display biggest of three numbers.
a b c
Declare
a number(2):=&a;
b number(2):=&b;
c number(2):=&c;
Begin
if a>b then
if a>c then
dbms_output.put_line('A is Big.');
else
dbms_output.put_line('C is Big.');
end if;
else
if b>c then
dbms_output.put_line('B is Big.');
else
dbms_output.put_line('C is Big.');
end if;
end if;
End;
else
<exec-statements>;
end if;
Example:
--------
-- pl/sql block for to display student grades.
Declare
tavg Number(7,2):=&Tavg;
Begin
if tavg>=70 then
dbms_output.put_line('Grade A.');
elsif tavg>=60 then
dbms_output.put_line('Grade B.');
elsif tavg>=50 then
dbms_output.put_line('Grade C.');
else
dbms_output.put_line('Grade D.');
end if;
End;
syntax:
-------
Case <variable/Expression>
when <constant> then
<exec-statements>;
when <constant> then
<exec-statements>;
when <constant> then
<exec-statements>;
else
<exec-statement>;
end case;
example:
--------
Declare
color Varchar2(1):='&color';
Begin
case color
when 'r' then
dbms_output.put_line('u r selected Red Color.');
when 'y' then
dbms_output.put_line('u r selected Yellow Color.');
when 'b' then
dbms_output.put_line('u r selected Blue Color.');
else
dbms_output.put_line('u r selection is wrong, plz. try again!!!!!');
end case;
End;
i. Simple Loop:
---------------
-> it is an infinite loop.
syntax:
-------
Loop
<exec-statements>;
End Loop;
example:
--------
Begin
Loop
dbms_output.put_line('Welcome');
End Loop;
End;
example:
-------
Declare
i Number(2):=1;
Begin
Loop
if i<=10 then
dbms_output.put_line('Welcome');
else
exit;
end if;
i:=i+1;
End Loop;
End;
syntax2:
--------
EXIT when <condition>;
example:
-------
Declare
i Number(2):=1;
Begin
Loop
Exit when i>10;
dbms_output.put_line('Welcome');
i:=i+1;
End Loop;
End;
Example:
-------
-- write a pl/sql block to print first 10 natural numbers.
Declare
i Number(2):=1;
Begin
while(i<=10)
Loop
dbms_output.put_line(i);
i:=i+1;
End Loop;
End;
-- write a pl/sql block to print multiplication table.
5*1=5
5*2=10
.
.
5*10=50
Declare
N Number(2):=&N;
i Number(2):=1;
Begin
While(i<=10)
Loop
dbms_output.put_line(N||'*'||i||'='||(N*i));
i:=i+1;
End Loop;
End;
Declare
i Number(2):=10;
Begin
while(i>=1)
Loop
dbms_output.put_line(i);
i:=i-1;
End Loop;
End;
Example:
-------
Begin
for i in 1..10
loop
dbms_output.put_line(i);
end loop;
End;
-- in reverse order
Begin
for i in Reverse 1..10
loop
dbms_output.put_line(i);
end loop;
End;
CURSORS:
--------
Declare
e emp%rowtype;
Begin
select * into e from emp where DEPTNO=&DEPTNO;
dbms_output.put_line(e.empno||' '||e.ename||' '||e.sal||' '||e.deptno);
End;
Cursors:
-------
-> it is one of the private context area.
-> it creates a temporary buffer
-> it is created by using select statement output.
-> it is not stored in db permanently.
-> in pl/sql block whenever select statement returns morethan one record, on that time we are using
cursors concept.
-> basically cursors are two types.
I. Static Cursor
i. Explicit Cursors
ii. Implicit Cursors
II. Dynamic Cursor
I. Static Cursor
i. Explicit Cursors:
--------------------
-> it is created by User
cursor Operations:
-----------------
i. Declaring the Cursor:
-----------------------
-> in declaration part we can declare the cursor
syntax:
------
cursor <cursor_name> is <select statement>;
ii. Open <cursor_name>:
----------------------
-> it opens the cursor
-> Memory is allocated, After opened.
cursor Attributes:
-----------------
-> it shows status of the cursor
-> it returns boolean value.
syntax:
-------
<cursor_name>%<attribute>;
a. %isopen:
----------
-> it returns true, when the cursor opens successfully.
b. %found:
----------
-> it returns true, when the cursor contains data.
c. %notfound:
-------------
-> it returns true, when the cursor doesn't find any data.
d.%rowcount:
------------
-> it returs number.
-> no.of fetch statements executed.
Example:
--------
Declare
cursor my_cur is select empno,ename,sal,deptno from emp;
tempno emp.empno%type;
tename emp.ename%type;
tsal emp.sal%type;
tdeptno emp.deptno%type;
Begin
Open my_cur;
if my_cur%isopen then
dbms_output.put_line('cursor opens successfully.');
end if;
fetch my_cur into tempno,tename,tsal,tdeptno;
dbms_output.put_line(tempno||' '||tename||' '||tsal||' '||tdeptno);
fetch my_cur into tempno,tename,tsal,tdeptno;
dbms_output.put_line(tempno||' '||tename||' '||tsal||' '||tdeptno);
fetch my_cur into tempno,tename,tsal,tdeptno;
dbms_output.put_line(tempno||' '||tename||' '||tsal||' '||tdeptno);
Close my_cur;
End;
--> write a cursor program to print first five max. salaries employee details.
--> write a cursor to displa alternative records.(even or odd records)
-> Create table Library Database with Book_id, Book_name, Author and Stock.
validations:
------------
-> Book_id should be generated automatically..
-> Book_name is Capital Letters
-> Author name is Initial Capital Letter
-> Min. Stock 1 and Max. Stock 10 should be accepted.
Create a Student Database with Sno, sname, Class, Book_id, No.of issued BOOKS
,Issue Date, Return Date.
Validations:
-----------
-> Sname is Capital Letters.
-> Class should be accept only 1 t0 10.
-> Book_id must be present in Library.
-> min. and max. No.of book issue to student only 1.
-> Date of issue is not Grater than System date and Not Less than System date.
-> Date of Return Should Grater than or equal to Date of Issue.
-> if Book is Issuing or returning stock should be updated.
Trigger:
-------
Trigger Event: Before Insert
Trigger Name: Lib_Before_insert
Trigger type: Row Level Triggers
Table Name: Library
Create or Replace Trigger Lib_Before_Insert
Before Insert on Library
for each row
begin
Select Nvl(Max(Book_id),0)+1 into :new.book_id from Library;
:new.book_name:=upper(:new.book_name);
:new.Author:=initcap(:new.author);
if :new.stock not between 1 and 10 then
raise_application_error(-20001,'Stock Accepts only 1 to 10.');
end if;
End;
Triggers:
---------
Trigger Event: Before Insert
Table Name: Student
Trigger Name: stu_before_insert
-> All Named Blocks Programs(triggers, procedures, functions & packages) are stored in
'User_Source'(pre-defined table).
-> to see the trigger body
Dropping Triggers:
------------------
Drop Trigger <Trigger_name>;
ex:
---
Drop Trigger Stu_Before_insert;
SUB PROGRAMS:
-> A SET OF PL/SQL STATEMENTS STORED PERMANENTLY IN DATABASE AND PERFORMS A TASK.
-> THEY ACCEPT ARGUMENTS WHILE EXECUTION
-> THEY ARE STORED IN 'COMPILED FORMAT';
-> THEY ARE REUSABLE COMPONENTS
-> THEY PROVIDES MODULARITY
MODULARITY
-> A HUGE PROGRAM DEVIDED INTO SUB TASKS
-> EASY TO UNDERSTAND
-> EASY TO DEBUG THE ERRORS
1. PROCEDURES
-> Procedures are may/may not return value
2. FUNCTIONS
-> Functions should return value
Procedure syntax:
Create or Replace Procedure <Procedure_name>
[ (argument mode datatype,....)]
is
<variable declaration>;
Begin
<exec statements>;
[ Exception Block
<exec-statements>; ]
end;
2. Begin
<procedure_name>;
End;
ex: Begin
my_proc;
End;
ex: IN
-------
Create or replace procedure add_proc(a in number, b in number)
is
begin
dbms_output.put_line('sum of two numbers = '||(a+b));
end add_proc;
exec Add_proc(90,30);
ex: OUT :
----------
Create or replace procedure add_Proc(A in Number, B in Number, C out Number)
is
Begin
C:=A+B;
End add_proc;
to execute above procedure two methods:
---------------------------------------
1. on Sql prompt:
-----------------
sql> Var res Number;
-> to declare variables on sql prompt those variables are called as Global Variables/Bind variables/sql
buffer variables.
-> bind variables are represented with ':' operator.
3. IN OUT:
----------
Create or Replace Procedure Add_proc(a in out number)
is
Begin
a:=a*a*a;
End add_proc;
Result: ___120_____
Cal Exit
Dropping Procedures:
--------------------
'EVEN' or 'ODD'
void main()
{
a=10;b=20;
add(a,b);
}
add(int x, int y)
{
p(x+y);
};
Function Syntax :
End;
Create or Replace Procedure Emp_Proc(Temp Varchar2)
is
cursor Emp_cur is select * from emp;
e emp%rowtype;
begin
open Emp_cur;
Loop
fetch Emp_cur into e;
exit when Emp_cur%notfound;
if temp='even' and mod(emp_cur%rowcount,2)=0 then
dbms_output.put_line(emp_cur%rowcount||' '||e.empno||' '||e.ename||' '||e.sal||'
'||e.deptno);
elsif temp='odd' and mod(emp_cur%rowcount,2)=1 then
dbms_output.put_line(emp_cur%rowcount||' '||e.empno||' '||e.ename||' '||e.sal||'
'||e.deptno);
end if;
End Loop;
End Emp_proc;
PL/SQL BLOCK:
-------------
Declare
E Emp%rowtype;
status varchar2(50);
Begin
my_proc(&Empno,E,Status);
dbms_output.put_line(Status);
dbms_output.put_line(e.empno||' '||e.ename||' '||e.job||' '||e.sal||' '||e.deptno);
End;
EXAMPLE:
--------
Create or Replace Procedure Dept_Proc(Tdeptno Number,
Tdname Varchar2,
Tloc Varchar2)
is
begin
insert into Dept values(Tdeptno,Tdname,Tloc);
End Dept_proc;
Notations:
----------
-> there are two types of notations.
i. positional Notations
ii. Named Notations.
i. positional Notations:
------------------------
-> to pass the arguments according their argument positions while executing the program.
Dropping Functions:
------------------
Drop Function <function_name>;
Ex:
Drop Function Emp_Expe;
PACKAGES :
It is a collection of Variables, Cursors,
Procedures & Functions are stored in one location.
Easy to share the Subprograms in Application S/w Tools
They Improve performance while accessing subprograms from client location
They are stored in User_Source system table
They supports function Overloading, Encapsulation & Databinding
It has two parts
1. Package Specification :
It holds the declaration of Variables, Cursors & Subprograms
2. Package Body:
It holds the body of subprograms
Packages:
---------
Example:
-------
Package Specification:
---------------------
Create or replace package my_pack
is
Result Varchar2(50); -- public variables
Procedure emp_exp(tempno emp.empno%type);
Function emp_netsal(Tempno Emp.Empno%type) return Varchar2;
End my_pack;
Package Body:
------------
Create or Replace Package Body my_pack
is
Procedure Emp_Exp(Tempno Emp.Empno%type)
is
Tdate Emp.Hiredate%type; -- private variables
Texp Number;
Begin
Select Hiredate into Tdate from Emp where Empno=Tempno;
Texp:=round((sysdate-tdate)/365);
dbms_output.put_line(Tempno||' Employee Experience is '||Texp||'Years.');
End Emp_Exp;
Function Overloading:
--------------------
Create or Replace Package fo_pack
is
Function addval(a number, b number) return Number;
Function addval(a number, b number, c number) return number;
Function addval(str1 varchar2, str2 varchar2) return Varchar2;
Function addval(str1 varchar2, str2 varchar2, str3 varchar2) return varchar2;
End fo_pack;
Package Body:
-------------
Create or Replace Package Body fo_pack
is
Function addval(a number, b number) return Number
is
Begin
return(a+b);
End addval;
End fo_pack;
Dropping Packages:
-----------------
Drop Package my_pack;
Packages using Dynamic cursors:
------------------------------
Create or Replace Package p1
is
Type Rcur is Ref Cursor;
End p1;
Procedure:
---------
Create or Replace Procedure my_proc(Tdeptno in Emp.Deptno%type,
my_cur out p1.Rcur)
is
Begin
Open my_cur for Select * from emp where Deptno=Tdeptno;
End my_proc;
pl/sql block:
------------
Declare
my_cur p1.Rcur;
e emp%rowtype;
Begin
my_proc(&tdeptno,my_cur);
Loop
fetch my_cur into E;
exit when my_cur%notfound;
dbms_output.put_line(e.empno||' '||e.ename||' '||e.job||' '||e.sal||
' '||e.deptno);
End Loop;
End;
Create a Bank Database with Accno,customer Name, Account type, Opening Date,Balance.
validations:
------------
-> Account no should be generated automatically(ex: sbi1, sbi2,....)
-> Customer name is capital Letters.
-> Opening date is system date
-> account type should be accepts only either 's' or 'c'
-> if account type is savings then Min. Balance is Rs.500/-
-> if account type is current then min. balance is Rs.1000/-.
Create a bank Transaction table with Transaction Account Number, Transaction Date,
Transaction type and Transaction Amount.
Validations:
-----------
-> Taccount Number should be present in Bank Database.
-> Tdate is System date
-> Transaction type should be accepts either 'D' or 'W'
-> Min. Trasaction Amount is Rs.100/-
-> if the customer applying withdraw, check the available balance.
-> if Customer withdrawing or Depositing amount balance should be updated.
Packages:
---------
Example:
-------
Package Specification:
---------------------
Create or replace package my_pack
is
Result Varchar2(50); -- public variables
Procedure emp_exp(tempno emp.empno%type);
Function emp_netsal(Tempno Emp.Empno%type) return Varchar2;
End my_pack;
Package Body:
------------
Create or Replace Package Body my_pack
is
Procedure Emp_Exp(Tempno Emp.Empno%type)
is
Tdate Emp.Hiredate%type; -- private variables
Texp Number;
Begin
Select Hiredate into Tdate from Emp where Empno=Tempno;
Texp:=round((sysdate-tdate)/365);
dbms_output.put_line(Tempno||' Employee Experience is '||Texp||'Years.');
End Emp_Exp;
Function Overloading:
--------------------
Create or Replace Package fo_pack
is
Function addval(a number, b number) return Number;
Function addval(a number, b number, c number) return number;
Function addval(str1 varchar2, str2 varchar2) return Varchar2;
Function addval(str1 varchar2, str2 varchar2, str3 varchar2) return varchar2;
End fo_pack;
Package Body:
-------------
Create or Replace Package Body fo_pack
is
Function addval(a number, b number) return Number
is
Begin
return(a+b);
End addval;
End fo_pack;
Select fo_pack.addval(10,20) from dual;
Dropping Packages:
-----------------
Drop Package my_pack;
Procedure:
---------
Create or Replace Procedure my_proc(Tdeptno in Emp.Deptno%type,
my_cur out p1.Rcur)
is
Begin
Open my_cur for Select * from emp where Deptno=Tdeptno;
End my_proc;
pl/sql block:
------------
Declare
my_cur p1.Rcur;
e emp%rowtype;
Begin
my_proc(&tdeptno,my_cur);
Loop
fetch my_cur into E;
exit when my_cur%notfound;
dbms_output.put_line(e.empno||' '||e.ename||' '||e.job||' '||e.sal||
' '||e.deptno);
End Loop;
End;
Create a Bank Database with Accno,customer Name, Account type, Opening Date, Balance.
validations:
------------
-> Account no should be generated automatically(ex: sbi1, sbi2,....)
-> Customer name is capital Letters.
-> Opening date is system date
-> account type should be accepts only either 's' or 'c'
-> if account type is savings then Min. Balance is Rs.500/-
-> if account type is current then min. balance is Rs.1000/-.
Create a bank Transaction table with Transaction Account Number, Transaction Date,
Transaction type and Transaction Amount.
Validations:
-----------
-> Taccount Number should be present in Bank Database.
-> Tdate is System date
-> Transaction type should be accepts either 'D' or 'W'
-> Min. Trasaction Amount is Rs.100/-
-> if the customer applying withdraw, check the available balance.
-> if Customer withdrawing or Depositing amount balance should be updated.
Create a Package:
----------------
Create or replace package bank_pack
is
Function Chk_Bal(Taccno Trans.Taccno%type,
Tamount Trans.Tamount%type) Return Boolean;
Procedure Upd_Bal(Taccno Trans.Taccno%type,
Ttype Trans.Ttype%type,
Tamount Trans.Tamount%type);
End Bank_Pack;
Package Body:
-------------
Create or Replace Package Body Bank_Pack
is
Function Chk_Bal(Taccno Trans.Taccno%type,Tamount Trans.Tamount%type)
Return Boolean
is
Tbal Bank_mas.Balance%type;
Tacc_type Bank_mas.Acc_type%type;
Begin
Select Acc_type,Balance into Tacc_type, Tbal from Bank_mas
where Accno=Taccno;
if Tacc_type in ('s','S') then
if(tbal-Tamount)>=500 then
Return(True);
else
Return(False);
end if;
elsif Tacc_type in ('c','C') then
if(tbal-tamount)>=1000 then
Return(True);
else
Return(False);
End if;
End if;
End chk_bal;
i. Simple Loop:
---------------
-> it is an infinite loop.
syntax:
-------
Loop
<exec-statements>;
End Loop;
example:
--------
Begin
Loop
dbms_output.put_line('Welcome');
End Loop;
End;
example:
-------
Declare
i Number(2):=1;
Begin
Loop
if i<=10 then
dbms_output.put_line('Welcome');
else
exit;
end if;
i:=i+1;
End Loop;
End;
syntax2:
--------
EXIT when <condition>;
example:
-------
Declare
i Number(2):=1;
Begin
Loop
Exit when i>10;
dbms_output.put_line('Welcome');
i:=i+1;
End Loop;
End;
Example:
-------
-- write a pl/sql block to print first 10 natural numbers.
Declare
i Number(2):=1;
Begin
while(i<=10)
Loop
dbms_output.put_line(i);
i:=i+1;
End Loop;
End;
Declare
N Number(2):=&N;
i Number(2):=1;
Begin
While(i<=10)
Loop
dbms_output.put_line(N||'*'||i||'='||(N*i));
i:=i+1;
End Loop;
End;
Declare
i Number(2):=10;
Begin
while(i>=1)
Loop
dbms_output.put_line(i);
i:=i-1;
End Loop;
End;
syntax:
-------
for <index_variable> in <start_value>..<end_value>
Loop
<exec-statements>;
End Loop;
Example:
-------
Begin
for i in 1..10
loop
dbms_output.put_line(i);
end loop;
End;
-- in reverse order
Begin
for i in Reverse 1..10
loop
dbms_output.put_line(i);
end loop;
End;
CURSORS:
--------
Declare
e emp%rowtype;
Begin
select * into e from emp where DEPTNO=&DEPTNO;
dbms_output.put_line(e.empno||' '||e.ename||' '||e.sal||' '||e.deptno);
End;
Cursors:
-------
-> it is one of the private context area.
-> it creates a temporary buffer
-> it is created by using select statement output.
-> it is not stored in db permanently.
-> in pl/sql block whenever select statement returns morethan one record, on that time we are using
cursors concept.
I. Static Cursor:
i. Explicit Cursors:
--------------------
-> it is created by User
cursor Operations:
-----------------
i. Declaring the Cursor:
-----------------------
-> in declaration part we can declare the cursor
syntax:
-----
cursor <cursor_name> is <select statement>;
cursor Attributes:
-----------------
-> it shows status of the cursor
-> it returns boolean value.
syntax:
-------
<cursor_name>%<attribute>;
a. %isopen:
----------
-> it returns true, when the cursor opens successfully.
b. %found:
----------
-> it returns true, when the cursor contains data.
c. %notfound:
-------------
-> it returns true, when the cursor doesn't find any data.
d.%rowcount:
------------
-> it returs number.
-> no.of fetch statements executed.
Example:
--------
Declare
cursor my_cur is select empno,ename,sal,deptno from emp;
tempno emp.empno%type;
tename emp.ename%type;
tsal emp.sal%type;
tdeptno emp.deptno%type;
Begin
Open my_cur;
if my_cur%isopen then
dbms_output.put_line('cursor opens successfully.');
end if;
fetch my_cur into tempno,tename,tsal,tdeptno;
dbms_output.put_line(tempno||' '||tename||' '||tsal||' '||tdeptno);
fetch my_cur into tempno,tename,tsal,tdeptno;
dbms_output.put_line(tempno||' '||tename||' '||tsal||' '||tdeptno);
fetch my_cur into tempno,tename,tsal,tdeptno;
dbms_output.put_line(tempno||' '||tename||' '||tsal||' '||tdeptno);
Close my_cur;
End;
Open my_cur;
if my_cur%isopen then
dbms_output.put_line('cursor opens successfully.');
end if;
Loop
fetch my_cur into tempno,tename,tsal,tdeptno;
Exit when my_cur%notfound;
dbms_output.put_line(tempno||' '||tename||' '||tsal||' '||tdeptno);
End Loop;
Close my_cur;
End;
--> write a cursor program to print first five max. salaries employee details.
Declare
cursor my_cur is select empno,ename,job,hiredate,sal,deptno from emp order by sal desc;
e my_cur%rowtype;
Begin
Open my_cur;
Loop
Fetch my_cur into e;
Exit when my_cur%rowcount>5;
dbms_output.put_line(e.empno||' '||e.ename||' '||e.sal||' '||e.hiredate||' '||e.deptno);
End Loop;
Close my_cur;
End;
Declare
cursor emp_cur(pdeptno number) is select * from emp where deptno=pdeptno;
e emp%rowtype;
Begin
Open emp_cur(10);
Loop
Fetch emp_cur into e;
Exit when emp_cur%notfound;
dbms_output.put_line(emp_cur%rowcount||' '||e.empno||' '||e.ename||' '||e.sal||
' '||e.deptno);
End Loop;
Close emp_cur;
End;
Static Cursor:
-------------
-> while declaring the cursor to provide select statement is called as Static cursor.
example:
-------
Declare
Type R_cur is Ref Cursor;
my_cur R_cur;
e emp%rowtype;
d dept%rowtype;
Begin
Open my_cur for select * from emp;
Loop
fetch my_cur into e;
dbms_output.put_line(e.empno||' '||e.ename||' '||e.sal||' '||e.hiredate||' '||e.deptno);
Exit when my_cur%notfound;
End Loop;
Close my_cur;
Open my_cur for select * from dept;
Loop
fetch my_cur into d;
dbms_output.put_line(d.deptno||' '||d.dname||' '||d.loc);
Exit when my_cur%notfound;
End Loop;
Close my_cur;
End;
FOR CURSOR:
----------
Declare
cursor emp_cur is select * from emp;
Begin
for i in emp_cur
Loop
dbms_output.put_line(i.empno||' '||i.ename||' '||i.sal||' '||i.hiredate||' '||i.deptno);
End loop;
End;
EXCEPTION HANDLING:
------------------
-> PL/SQL errors are called as Exceptions.
-> Exceptions are classified into 3 types.
I. Pre-Defined Exceptions
II. User-Defined Exceptions
III. Non-Pre-Defined Exceptions
Declare
E Emp%rowtype;
Begin
select * into e from Emp where Empno=&Empno;
dbms_output.put_line(e.empno||' '||e.ename||' '||e.sal||' '||e.deptno);
End;
I. Pre-Defined Exceptions:
-------------------------
-> Exception Name is Defined by Oracle
-> Activated by Oracle
-> Solution is Provided by User
Declare
E Emp%rowtype;
Begin
select * into e from Emp where Empno=&Empno;
dbms_output.put_line(e.empno||' '||e.ename||' '||e.sal||' '||e.deptno);
Exception
when no_data_found then
raise_application_error(-20001,'Such Employee Number Not Exists.');
End;
Example2:
--------
Declare
E Emp%rowtype;
Begin
select * into e from Emp where deptno=&deptno;
dbms_output.put_line(e.empno||' '||e.ename||' '||e.sal||' '||e.deptno);
Exception
when no_data_found then
raise_application_error(-20001,'Such Employee Number Not Exists.');
when too_many_rows then
raise_application_error(-20002,'Morethan One Employee Exists..');
End;
example:
--------
Declare
Tsal Emp.Sal%type;
Tcomm Emp.Comm%type;
Netsal Number;
Nulls_Found Exception;
Begin
select Sal,Comm into Tsal,Tcomm From Emp where Empno=&Tempno;
if Tcomm is Null then
Raise Nulls_Found;
end if;
Netsal:=Tsal+Tcomm;
dbms_output.put_line('Salary : '||Tsal);
dbms_output.put_line('Comm : '||Tcomm);
dbms_output.put_line('Net Salary : '||Netsal);
Exception
When Nulls_Found then
Raise_application_error(-20001,'Comm Column value is Null.');
When No_Data_Found then
Raise_application_error(-20002,'Such Employee Number Not Exists');
End;
III. Non-Predefined Exceptions:
------------------------------
-> these exceptions are raised whenever constraint voilated errors occurs.
-> Exception Name is Defined by User
-> Activated by Oracle
-> Solution is provided by User
Declare
Duplicates_Found Exception;
pragma Exception_init(Duplicates_found,-00001);
Begin
Insert into Dept values(&Deptno,'&dname','&loc');
Exception
when Duplicates_found then
raise_application_error(-20001,'Such Department Number is Already Exists.');
End;
Example2:
--------
Declare
Duplicates_Found Exception;
pragma Exception_init(Duplicates_found,-00001);
Nulls_Found Exception;
pragma Exception_init(Nulls_found,-1400);
Begin
Insert into Dept values(&Deptno,'&dname','&loc');
Exception
when Duplicates_found then
raise_application_error(-20001,'Such Department Number is Already Exists.');
when nulls_found then
raise_application_error(-20002,'Department Number is Mandatory...');
End;
composite Datatypes:
-------------------
Composite Datatypes:
? User defined Datatypes
? Created by user in PL/SQL Block
? Valid in PL/SQL only
? They are not stored in database
They are not Re-usable & Shared
? They will not hold data
? They improve performance of Oracle while
Manipulating data in PL/SQL block
2 types
1. PL/SQL Record
2. PL/SQL Table
1. PL/SQL Record
i. It is a collect of elements of different data types and stored at one location
ii. it is similar to C structure
Syntax:
Type <recordname> is record
( Element1 datatype,
Element2 datatype,
..,
ElementN datatype);
example:
---------
Declare
Type Erec is record
(
Empno Number(4),
Name varchar2(10),
Basic Number(7,2),
Job Varchar2(10),
Doj Date,
Deptno Number(2));
e erec;
Begin
e.empno:=&empno;
select Empno,Ename,Sal,Job,Hiredate,Deptno into e.Empno,e.name,E.Basic,e.job,
e.doj,e.deptno from emp where empno=e.empno;
dbms_output.put_line(e.empno||' '||e.Name||' '||e.Job||' '||e.doj||' '||e.deptno);
End;
2.PL/SQL Table
Syntax:
Declare
Type Name is table of Emp.Ename%type index by Binary_Integer;
Type Pay is table of Emp.sal%type index by Binary_Integer;
N Name;
P Pay;
Cnt Number:=0;
Begin
-- filling pl/sql tables
for i in (select ename,sal from emp)
Loop
N(cnt):=i.Ename;
P(cnt):=i.Sal;
cnt:=cnt+1;
End Loop;
for k in N.first..N.last
loop
dbms_output.put_line(n(k)||' '||P(k));
end Loop;
End;
TRIGGERS :
Triggers:
? A set of PL/SQL statements stored permanently in database and Automatically activated
when ever an EvenT Raising statement(DML) is performed.
? They are used to impose user defined Restrictions(or)Business Rules on Tables.
? They are also activated when Tables are manipulated by other users or by other application S/w tools.
? They provide High Security on tables
? They are stored in USER_TRIGGERS system table
Trigger Parts:
1. Trigger Event : Indicates when to activate the Trigger
Before
Insert, Update, Delete = 6 Events
After
3. Trigger Restriction :
4. Trigger Body :
Example:
--------
-> to increment employee salary automatically.
incr:
empno amount
7788 1000
-> Create table Library Database with Book_id, Book_name, Author and Stock.
validations:
------------
-> Book_id should be generated automatically..
-> Book_name is Capital Letters
-> Author name is Initical Capital Letter
-> Min. Stock 1 and Max. Stock 10 should be accepted.
-> Create a Student Database with Sno, sname, Class, Book_id, No.of issued,Issue Date, Return Date.
Validations:
-----------
-> Sname is Capital Letters.
-> Class should be accept only 1 t0 10.
-> Book_id must be present in Library.
-> min. and max. No.of book issue to student only 1.
-> Date of issue is not Grater than System date and Not Less than System date.
-> Date of Return Should Grater than or equal to Date of Issue.
-> if Book is Issuing or returning stock should be updated.