Professional Documents
Culture Documents
3Y It is procedural.
3Y It supports all the datatypes of SQL.
3Y Instead of sending ͚n͛ number of SQL statements they can be binded into PL/SQL block which
minimises network traffic.
c
1.Y eclaration
2.Y Executable statements
3.Y Exception handling
eclare
ec 1;
ec 2;
Begin
Statement 1;
Statement 2;
Exceptions
Exec 1;
Exec 2;
End;
To run it sql>@filename.sql
^
!
These blocks are stored directly into database, the advantage is every user who is connected to
database can access these blocks.
1)procedures
2)functions
3)triggers
4)packages
3Y Ôll the variables used should be declared in the declaration part itself.
3Y To display the messages on screen we use
put_line : is the function in this package which is used for displaying info on screen.
put_line:
Is a function which takes single parameter as input, if at all user wants to give 2 values then we
use concatenation operator which combines 2 values and converts into single value ||.
0 "c 0
declare
i number:=&enter_val;
begin
dbms_output.put_line('val i='||i);
end;
1):= IS Ô ÔSSIGNMENT OPERÔTOR WITH WHICH THE VÔLUES WILL BE ÔSSIGN TO VÔRIÔBLES.
1.Y declare
age number:=&enter_age;
begin
if(age>=18) then
else
end if;
end;
2.Y declare
mavg number:=&enter_avg;
begin
If(mavg>=60) then
else
end if;
end;
3Y While loop.
3Y Loop exit.
3Y For loop.
ECLÔRE
I NUMBER;
BEGIN
I:=1;
WHILE(I<=5) LOOP
BMS_OUTPUT.PUT_LINE('VÔL I='||I);
I:=I+1;
EN LOOP;
EN;
'
ECLÔRE
I NUMBER;
BEGIN
I:=1;
LOOP
BMS_OUTPUT.PUT_LINE('VÔL I='||I);
I:=I+1;
EN LOOP;
EN;
3Y BEGIN
BMS_OUTPUT.PUT_LINE('VÔL K='||K);
EN LOOP;
EN;
3Y BEGIN
BMS_OUTPUT.PUT_LINE('VÔL K='||K);
EN LOOP;
EN;
c !
1.Y (
: refers to the datatype of a particular column of a table.
2.Y )(
refers to the entire row of the table.
Is defined as a PL/SQL memory area or buffer used to store the records fetched by select
statement. The buffer name is the cursor name.
The adv of this concept is unlike variables the work area can store multiple rows.
)
+Y Implicit cursor: This cursor will be used by sql itself for processing client request.
,Y Explicit cursor.
Y Simple cursor.
Y Ref cursor.
(a)Y Strong cursor.
(b)Y Weak cursor.
we have cursor attributes which are used for getting the position of cursor in work area.
3Y isopen: Returns true if the cursor is already open else returns false.
3Y found : Return true if there is any row exists at current cursor location else it returns
false.
3Y notfound: Works opposite to found i.e, it return false if there is any row exists at current
cursor location else it returns true.
3Y rowcount: Returns total no of rows in work area.
3)open cursor
5)close cursor .
c
-
The advantage of this cursor is we can pass the parameter at runtime so that we
can have selected rows in cursor work area.
!c
Ôre the program units which help us to write piece of code which can be called
anywhere or anytime in another pl/sql block or subprogram.
+c
Is a subprogam which will not return any value
Syntax:
Var dec
Begin
Statements
End;
Syntax:
Var dec
Begin
Statements
Return type_res
End;
var declaration
begin
statements
exception
end;
c
It is a collection of procedure and functions
c
Ôre used to pass some input to sub program so that we can get some dynamic output
+By default all parameter are "in" type i.e, we can pass input only
we can pass the values or get the result by this type of parameter.
0'
Is defined as an erroneous event which occurs during the program execution. In Oracle an
error condition is said to be an EXCEPTION.
Exception
End;
Exception
>Y
Raises when in a variable the datavalue is more than it͛s size is inserted.
´
'
This exception is externally declared by the user, raised by the user and handled by the user.
3Y When exception arises the execution of the program gets terminated and the cursor moves to
the exception part.
declare
qty_is_less_than_required exception;
qord number:=&enter_qord;
prow prodrowtype;
prodno varchar2(5) :='&enter_pno';
begin
select * into prow from prod where pno=prodno;
if( prow.pqty<qord) then
raise qty_is_less_than_required;
end if;
update prod set pqty=pqty-qord where pno=prodno;
commit;
exception
when qty_is_less_than_required then
dbms_output.put_line('qty order is more than qty on hand');
end;
0'
//
Ô named exception can be associated with a particular oracle error. This gives the ability to trap
the error rather than via error handlers.
( '
0'
eclare
rec dept10rowtype;
up_key exception;
Pragma.exception_init(dup_key,-1);
Begin
rec.deptno:=&deptno;
rec.dname:=&dname;
Exception
opl(sqlerrm);
End;
/
/
This is used for assigning oracle error numbers to user defined messages.
c
1.Package specification.
2.Package body.
c
eclaration of exceptions
eclaration of procedures
eclaration of cursors
eclaration of functions
End;
c
!(
efinition of procedure
efinition of function
efinition of exception
efinition of cursor
End;
^
+The package name should be same in both package specification and package body.
Sql>@filename.sql
0'
c
Negativebalance exception;
Minbalance exception;
End;
c
!(
If oamt<500 then
Raise minbal;
End if;
Exception
End;
Return number is
Ômt number;
Begin
where accno=no;
return amt+tamt;
end;
Return number is
Ômt number
Begin
Where accno=no;
If amt-500<tamt then
Raise negativebal;
Else
Return amt-tamt;
End if;
Exception
Return ;
End;
Cb number;
Begin
If trtypein(͚w͛,͛W͛) then
Cb:=withdraw(no,tamt);
Else
Cb:=deposit(no,tamt);
End if;
End;
Begin
elete from banktran where accno=no;
End;
Is
Begin
End loop;
End;
End;
+ eclare
No number:=&no;
Name varchar2(10);
Opendate date:=͛&opdate͛;
Opamt:=͛&opamt͛;
Begin
Bankpack.accopen(no,name,opdate,opamt);
opl(͚acc open͛);
End;
, declare
No number:=&no;
Ttype char:=͛&ttype͛;
Tamt number:=&tamt;
Ch char:=͛&tr-trlist-c1͛;
Begin
If ch=͛1͛ then
Bankpack.tramsaction(no,ttype,tamt);
opl(͚transaction completed͛);
opl(͚no͛);
Else
Bankpack.closeacc(no);
opl(͚acc closed͛);
Endif;
End;
#
Ô trigger is stored procedure which is executed on a table whenever a ML statement is issued
on that table. When an event occurs trigger fires.
0
(
'
Create or replace trigger <trigger name> <before/after><insert/update/delete> on
<table name> [for each row]
[when condition]
eclare
eclarations
Begin
Executable statements;
End;
/
)
0
#
( '
begin
end;
0
#
( '
Begin
End;
^
if u have both statement level trigger as well as row level trigger which are after then
the statement level trigger fires first.
!
!
Create or replace trigger trig3 before insert on bank for each row
eclare
Ô number;
Begin
:new.accno:=Ô+100;
End;
Note: ^
) statement is used to refer the new values of an insert or replace statement.
(
#
Create or replace trigger trig4 after insert or update on bank for each row
Begin
If :new.opdate>sysdate then
Raise application_error(-20010,͛opening date should always be less than or
equal to the sysdate͛);
End if;
End;
#
!
/*to store the details of the acc number which is deleted from the bank table */
Begin
End;
Create or replace trigger trig6 after insert or update or delete on bank for each row
eclare
U varchar2(10);
Begin
If inserting then
End if;
End;
/
"
When trigger statement and trigger action both are on the same table then a mutate table error
occcurs and that table can be called as mutate table.
Create or replace trigger trig7 before update on emp10 for each row
Begin
Where empno=7788;
End;
&
Create or replace trigger trig8 instead of insert on compview for each row
Begin
End;