Professional Documents
Culture Documents
PLSQL Day 3
PLSQL Day 3
*************
Database programs which are saved
under database server permenantly.
1) PROCEDURES
2) FUNCTIONS
i) Specification
************
It contains object type,object name,and
Input/output argument variables.
ii) Body
******
It contains declarations,begin block, exception
block, and end of procedure.
syntax:
/*Procedure specification*/
/* Procedure body */
<declaration stmts>;
BEGIN
-----
-----
EXCEPTION
--
--
END <proc_name>;
/ ( to compile the procedure in SQL * PLUS)
Procedure created.
1) By calling procedure
We can make a call to the procedure
--from program,procedure,function,package,and trigger.
Syntax:-
proc_name[(Argval1,Argval2,....)];
or
Properties of Procedure:-
*********************
--It can be created as a precompiled object.
--Means it will be compiled only once and can be
executed any number of times (reusability).
--we can modify existing Procedures (enhansibility).
--Procedures are created with or without arguments.
--By Default, Procedure cannot return any value.
b) Dynamic procedure
This is created with arguments,
means accepting runtime input values.
Argument variable:
****************
It is known as a variable to recieve runtime input value.
or to return output value.
--------------------------------------------------------------------------
STATIC PROCEDURE:
Ex:
write a procedure to display customer 1 account details?
Ex:
Write a procedure to find number of customers from the city
"Delhi"?
Ex:
BEGIN
proc_ecount_30; --Procedure calling stmt
dbms_output.put_line(' calling procedure is finished');
dbms_output.put_line(' Program Execution is finished');
end;
DYNAMIC PROCEDURES:
*******************
SAMPLE EXECUTIONS:
EXEC PROC_2(10);
EXEC PROC_2(20);
EXEC PROC_2(30);
ex:
declare
vdno int;
begin
vdno:=&vdno;
proc_ecount_did(vdno); --Procedure calling statement
dbms_output.put_line(' Exec Finished');
end;
declare
x int;
y int;
begin
x:=&x;
y:=&y;
dbms_output.put_line(' adding '||x ||' and '||y);
Dbms_output.put_line(' Making a call to P_ADD procedure');
p_add(x,y);
dbms_output.put_line(' Multiplication of '||x||' and '||y);
dbms_output.put_line(' make a call to P_MULTI procedure');
p_multi(x,y);
dbms_output.put_line(' 2 tasks successfully completed');
end;
Ex:
wap to display customer name,city,account name,actno and bal
from given customer id?
STATIC PROCEDURE:
Ex:
write a procedure to display the customer details and his account details for the
custid "cust-5"?
DYNAMIC PROCEDURE:
Ex:
write a procedure to display the customer details and his account
details for the GIVEN custid ?
Ex:
Ex:
write a procedure to display the customer details and his account
details for the GIVEN custid ?
STATIC PROCEDURE:
Ex: write a procedure to display the employee details and dept details for the
empno 7902?
DYNAMIC PROCEDURES:
EX: write a procedure to display the details of employee for the given employee id?
sample output:
Ex: write a procedure to display the customer mobile number and name of the given
custid if he is male and from the city delhi?
desc cust_dtls
Name Null Type
---------- ---- ------------
CUSTID CHAR(4)
CUSTNAME VARCHAR2(20)
CUSTCITY VARCHAR2(10)
CUSTGENDER CHAR(1)
CUSTMOBILE NUMBER(10)
Ex:
create or replace procedure proc_deposit
(
vactno cust_act_dtls.actno%type,
vamt number
)
is
vbal number;
begin
dbms_output.put_line
(chr(10)||' Account Number: '||vactno);
select act_bal into vbal
from cust_act_dtls
where actno=vactno;
dbms_output.put_line
(chr(10)||' Old balance: '||vbal);
update cust_act_dtls
set act_bal=act_bal+vamt
where actno=vactno;
commit;
select act_bal into vbal
from cust_act_dtls
where actno=vactno;
dbms_output.put_line
(chr(10)||' deposited amount: '||vamt||chr(10)||
' New balance: '||vbal||chr(10)||
'Transaction completed successfully'
);
end proc_deposit;
Ex: write a procedure to display number of emps working under given department
name?
Ex: write a procedure to display the number of customers from the given city with
given gender?
Ex: write a procedure to display the number of accounts of given account type?