PL / SQL程序-3

本文介绍了多个PL/SQL过程的实例,包括使用REF-CURSOR进行动态数据选择,输入输出模式的过程,以及在一个过程中调用另一个过程的例子。通过这些示例,读者可以了解如何在Oracle数据库中使用PL/SQL进行复杂的数据操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值