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

1:首先定义一个简单的包
create or replace package p1 is
procedure pro_p1;
Type p1_cursor is ref cursor;
end p1;

2:然后编写该包下的存储过程
create or replace package body p1 is
procedure pro_p1 is
--定义
s varchar2(100);
p1_cursor is select ename from emp;
begin
for loop_cursor in t loop
dbms_output.put_line(loop_cursor.ename);
end loop;
exception
when no_data_found then
null;
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 语句

守候你守候我
浏览 181回答 3
3回答

眼眸繁星

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完全可以完成你例中的情况。这时是不需要用动态游标的。

慕盖茨4494581

--定义包头create or replace package p1 isprocedure pro_p1;Type p1_cursor is ref cursor;end p1;--定义包体create or replace package body p1 isprocedure pro_p1iss varchar2(100);cur p1.p1_cursor;beginopen cur for select ename from emp;loopfetch cur into s;exit when cur%NOTFOUND;dbms_output.put_line(s);end loop;exceptionwhen no_data_found thennull;end pro_p1;end p1;--调用存储过程beginp1.pro_p1;end;--运行结果SMITHALLENWARDJONESMARTINBLAKECLARKSCOTTKINGTURNERADAMSJAMESFORDMILLER

扬帆大鱼

给你个例子,你看看吧,实在太多了SQL> l1 declare2 cursor emp_cursor is3 select ename from emp;4 v_name emp.ename%type;5 begin6 open emp_cursor;7 fetch emp_cursor into v_name;8 while emp_cursor%found loop9 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> /
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Oracle