为了账号安全,请及时绑定邮箱和手机立即绑定

我总结了一下午都没弄明白,实在是好麻烦,求高手帮解决,非常感谢!

我总结了一下午都没弄明白,实在是好麻烦,求高手帮解决,非常感谢!

守候你守候我 2022-10-20 19:15:30
1:首先定义一个简单的包create or replace package p1 isprocedure pro_p1;Type p1_cursor is ref cursor;end p1;2:然后编写该包下的存储过程create or replace package body p1 isprocedure pro_p1 is--定义s varchar2(100);p1_cursor is select ename from emp;beginfor loop_cursor in t loopdbms_output.put_line(loop_cursor.ename);end loop;exceptionwhen no_data_found thennull;end;end p1;PLS-00103: 出现符号 "IS"在需要下列之一时: constant exception <an identifier> <a double-quoted delimited-identifier> table LONG_ double ref char time timestamp interval date binary national character nchar我的问题1:哪里的错误?2:能总结一下游标可以定义的位置和使用方法吗?我见过两种:cursor 游标名 is select 语句 写在declare内部通过函数参数传入进来,然后使用open 游标 for select 语句
查看完整描述

3 回答

?
眼眸繁星

TA贡献1873条经验 获得超9个赞

Type p1_cursor is ref cursor;
是定义了一个动态游标类型叫p1_cursor ,如同number表示数字类型一样。使用时要定义一个该类型的变量才可以,如:
varCur p1_cursor ;
然后再在程序体内部定义这个动态游标变量varCur是什么:
open varCur for 查询语句。

这种定义 方式 通常用于查询语句本身是个变量,比如根据不同的值确定从哪个表中来查时,表名是作为变量的,这时就必须使用动态游标了。在你这个简单的应用中其实是不需要使用动态游标的。
你例中的游标是固定的查询语句(select ename from emp),语句本身是固定的,即使加上查询条件,也并不需要使用动态游标,这种情况下可以直接在程序头declare段中定义即可:
cursor varCur(varName) is select ename from emp where ename=varname;

总结:1、动态游标需要先用定义type定义一个类型(如:Type p1_cursor is ref cursor),再使用这个类型定义一个游标变量(如:varCur p1_cursor);
2、动态游标一般用于查询语句中有变量存在的情况,在程序体内部进行游标的查询语句,如:
varSQL:='select ename from emp where ename=' || varName;
open varCur for varSQL;

3、直接使用cursor完全可以完成你例中的情况。这时是不需要用动态游标的。

查看完整回答
反对 回复 2022-10-24
?
慕盖茨4494581

TA贡献1850条经验 获得超11个赞

--定义包头
create or replace package p1 is
procedure pro_p1;
Type p1_cursor is ref cursor;
end p1;

--定义包体
create or replace package body p1 is
procedure pro_p1
is
s varchar2(100);
cur p1.p1_cursor;
begin
open cur for select ename from emp;

loop
fetch cur into s;
exit when cur%NOTFOUND;
dbms_output.put_line(s);
end loop;

exception
when no_data_found then
null;
end pro_p1;
end p1;

--调用存储过程
begin
p1.pro_p1;
end;

--运行结果
SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK
SCOTT
KING
TURNER
ADAMS
JAMES
FORD
MILLER


查看完整回答
反对 回复 2022-10-24
?
扬帆大鱼

TA贡献1799条经验 获得超9个赞

给你个例子,你看看吧,实在太多了
SQL> l
1 declare
2 cursor emp_cursor is
3 select ename from emp;
4 v_name emp.ename%type;
5 begin
6 open emp_cursor;
7 fetch emp_cursor into v_name;
8 while emp_cursor%found loop
9 dbms_output.put_line(v_name);
10 exit when emp_cursor%rowcount >= 5;
11 fetch emp_cursor into v_name;
12 end loop;
13 close emp_cursor;
14* end;
SQL> /


查看完整回答
反对 回复 2022-10-24
  • 3 回答
  • 0 关注
  • 180 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号