Sie sind auf Seite 1von 5

Stored procedure :

-it is a database object

-syntax:

Create or replace procedure <procedure-name> (para1 datatype, para2


datatype,…)

Is/as

Begin

Exception

End <procedure-name>;

Run stored procedure:

-execute <procedure-name>;

Parameter type:

1. Formal – parameters present in procedure

2. Actual – parameters at procedure execution

Store procedure types:

1. Static stored procedure- no user interaction, repeatedly shows


one o/p only
Ex:
create or replace procedure proc_cust_2
is
v_cno number;
v_cust customer%rowtype;
begin
v_cno :=2;
select *
into v_cust
from customer
where cno=v_cno;
dbms_output.put_line(‘customer Information’);
dbms_output.put_line(‘customer Name :’||v_cust.cname);
dbms_output.put_line(‘customer Id :’||v_cust.cno);
dbms_output.put_line(‘customer Address :’||v_cust.city);
end;
/

Exec proc_cust_2;
Procedure created.
customer Information
customer Name :BBB
customer Id :2
customer Addr :Baramati

PL/SQL procedure successfully completed.

2. Dynamic stored procedure


Syntax of parameter declaration:
<Proc-name>(para-name [mode] datatype, para-name2 [mode]
datatype)
Parameters are of three :(in context with mode)
1. In parameter:
- To pass/ input value to procedure
- It will be constant
- By default mode is “in”

Ex1: insert record into customer table using in operator(stored


procedure):

Create or replace procedure pro_insert_cust(v_cno in number,

v_cname in varchar2, v_city in varchar2)

is

begin

insert into customer values (v_cno, v_cname, v_city);

dbms_output.put_line(‘Record Inserted Successfully’);

end;

Execution methods for in parameter:

1. positional notation:

exec pro_insert_cust(8,'Sanjana','Daund')

2. named notations :
exec pro_insert_cust(v_cno=>8, v_cname=>'Sanjana',
v_city=>'Daund')

3. mixed notations:

exec pro_insert_cust(10, v_cname=>'Sanjana',


v_city=>'Daund');

2. Out parameter:
- Shows output
- It is like variable
- Syntax:
(Para-name out datatype)

CREATE OR REPLACE PROCEDURE pro_square(a IN number,


b out number)
IS
BEGIN
b := a*a;
END;
/

/*output
1. Using bind variable
SQL> variable z number;
SQL> exec pro_square(9,:z);

PL/SQL procedure successfully completed.

SQL> print z;
Z
----------
81

2. Using anonymous block

DECLARE
x number :=&number;
y number;
BEGIN
pro_square(x,y);
DBMS_OUTPUT.PUT_LINE('Square of '||x||' : '||y);
END;
/
Enter value for number: 9
old 2: x number :=&number;
new 2: x number :=9;
Square of 9: 81
3. In out parameter
- It behaves like constant at parameter level
- It behaves like variable inside procedure block

Das könnte Ihnen auch gefallen