使用REF-CURSOR的样品程序
===================================
create or replace procedure refex(mnum number)
as
--ref cursor is declared.
type c1 is ref cursor;
mname varchar2(10);
mid number(4);
--vc1 is a variable of type c1 ,which is a ref cursor.
vc1 c1;
begin
--depending on the user input the procedure selects data dynamically from separate tables using ref cursor..
if mnum=1 then
open vc1 for select empno,ename from emp;
elsif mnum=2 then
open vc1 for select deptno,dname from dept;
end if;
loop
fetch vc1 into mid,mname;
exit when vc1%notfound;
dbms_output.put_line(mid ||' '|| mname);
end loop;
--close the ref cursor.
close vc1;
end;
输入输出模式的过程示例
=============================================
--the same variable receive the value and returns the value after prcessing.
CREATE OR REPLACE procedure fact(a in out number)
is
b number:=1;
begin
for i in 1..a loop
b:=b*i;
end loop;
a:=b;
end;
从一个匿名块执行该过程
================================================== =
declare
x number;
begin
x:=&values;
--x is the input variable
fact(x);
--same x is the output also.
dbms_output.put_line(x);
end;
注意:-由于此过程同时包含IN和OUT模式参数,因此无法使用exec在SQL提示符下直接执行。
输入,输出和输出模式的过程示例示例
================================================== =======
Create Or Replace Procedure Ioio
(
Num Emp.empno%type,
Name Out Emp.ename%type,
Num1 In Out Number
)
As
Begin
Select Ename,sal Into Name,num1 From Emp Where Empno=num And Mgr =num1;
Dbms_output.put_line(name||' '||num1);
End;
执行
===============
Declare
N Varchar2(10);
N1 Int :=7566;
Begin
Ioio(7788,n,n1);
End;
显示在另一个过程中调用过程的示例
==================================================
create or replace procedure find(eno number, flag out boolean)
is
a number(2);
begin
--finds out the number of records in the table where empno = the user input.
select count(*) into a from emp where empno=eno;
if a=0 then
--if there is no such reacord already .set flag to FALSE.
flag:=FALSE;
else
--else to TRUE.
flag:=TRUE;
end if;
end;
此程序呼叫上一个
=====================================
create or replace procedure ins_emp(a number)
is
eno emp.empno%type;
fg boolean;
begin
--the input of this procedure is passed to the previous procedure.
eno:=a;
--abc is a label.
<<abc>>
--the previous procedure is called here.
find(eno,fg);
--depending on the existance of the record the previosu procedure set the flag variable to TRUE or FALSE
if fg=true then
--if flag is true then display the message
dbms_output.put_line(eno||' No already exists...');
--and increment the input value by 1
eno := eno+1;
--and go back to the label and keep trying in a loop till the value is not in the table.
goto abc;
else
--if flag is false insert the vale to the table.
insert into emp(empno) values(eno);
dbms_output.put_line('New empno inserted is : '||eno);
end if;
end;
注意:-由于此过程仅包含IN模式参数,因此可以直接在SQL提示符下执行。
From: https://bytes.com/topic/oracle/insights/654128-pl-sql-procedures-3-a