用游标输出,看看错在哪?

csm810902 2010-01-30 12:39:55
该函数功能是这样的:当输入一个部门编号时,输出该部门的员工的所有信息。(oracle自带scott用户下的emp表)
create or replace function c_re(v_deptno in emp.deptno%type) return c_cursor
is
c_cursor cursor;
cursor c_cursor is select * from emp where deptno=v_deptno;
v_emp c_cursor%rowtype;
begin
open c_cursor;
fetch c_cursor into v_emp;
return v_emp;
close c_cursor;
end;
编译后第一行报错为:1 PLS-00320:此表达式的类型声明不完整或格式不正确,请好手指点。怎么改正?或写出你的函数也行
...全文
275 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangpengpengwang 2012-08-16
  • 打赏
  • 举报
回复
应该是另一种游标吧
duqiangcise 2010-01-31
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 computermajorsmc 的回复:]
sys_refcursor,系统游标类型,可以替代自定义的任何游标类型,比如:
type c_cursor is ref cursor;
v_mycur c_cursor;
完全可以用
v_mycur sys_refcursor;
来代替

这里ref cursor是一种游标类型吗?能具体解释一下下面两句吗?
type c_cursor is ref cursor;
v_mycur c_cursor;
谢谢!
[/Quote]
type c_cursor is ref cursor; --自定义了一个游标类型c_cursor;
v_mycur c_cursor;--创建一个自定义游标(c_cursor)的变量v_mycur;
duqiangcise 2010-01-31
  • 打赏
  • 举报
回复
顶起。
computermajorsmc 2010-01-31
  • 打赏
  • 举报
回复
sys_refcursor,系统游标类型,可以替代自定义的任何游标类型,比如:
type c_cursor is ref cursor;
v_mycur c_cursor;
完全可以用
v_mycur sys_refcursor;
来代替

这里ref cursor是一种游标类型吗?能具体解释一下下面两句吗?
type c_cursor is ref cursor;
v_mycur c_cursor;
谢谢!
computermajorsmc 2010-01-31
  • 打赏
  • 举报
回复
自定义一个游标类型应该是这么写的啊:
cursor c_cursor;

我还觉得可以这样写:
type c_cursor is cursor;

这里为什么要加ref?
csm810902 2010-01-30
  • 打赏
  • 举报
回复
自已写出来了,呵呵
create or replace function c_re(v_deptno in emp.deptno%type) return sys_refcursor
is
po_result sys_refcursor;
begin
open po_result for
select * from emp where deptno=v_deptno;
return po_result;
end;
这里sys_refcursor是一种游标的类型吗?那refcursor是什么呢?
crazylaa 2010-01-30
  • 打赏
  • 举报
回复
sys_refcursor,系统游标类型,可以替代自定义的任何游标类型,比如:
type c_cursor is ref cursor;
v_mycur c_cursor;
完全可以用
v_mycur sys_refcursor;
来代替
silezaihuolai 2010-01-30
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 csm810902 的回复:]
自已写出来了,呵呵
create or replace function c_re(v_deptno in emp.deptno%type) return sys_refcursor
is
  po_result sys_refcursor; 
begin
  open po_result for
  select * from emp where deptno=v_deptno; 
  return po_result;
end;
这里sys_refcursor是一种游标的类型吗?那refcursor是什么呢?
[/Quote]
sys_refcursor是系统定义的一种游标,至于refcursor应该是ref cursor吧。自己可以定义自己的游标类型的,比如 type c_cursor is ref cursor。
rfb0204421 2010-01-30
  • 打赏
  • 举报
回复
看见很多人调试过程依然使用DBMS_OUTPUT.PUT_LINE进行着输出,或是对oracle procedure返回resultset比较疑惑,下面的例子仅供参考。



Sql代码
CREATE OR REPLACE PROCEDURE sp_test (
p_outstr OUT VARCHAR2
,p_outint OUT NUMBER
,p_ref1 OUT SYS_REFCURSOR
,p_ref2 OUT SYS_REFCURSOR
)
AS
BEGIN
p_outstr := 'abc';
p_outint := '56789';
OPEN p_ref1 FOR SELECT ROWNUM*2 AS RN FROM DUAL CONNECT BY ROWNUM<=10;
OPEN p_ref2 FOR SELECT ROWNUM*2+1 AS RN FROM DUAL CONNECT BY ROWNUM<=10;

END sp_test;

/

过程已创建。

CREATE OR REPLACE PROCEDURE sp_test (
p_outstr OUT VARCHAR2
,p_outint OUT NUMBER
,p_ref1 OUT SYS_REFCURSOR
,p_ref2 OUT SYS_REFCURSOR
)
AS
BEGIN
p_outstr := 'abc';
p_outint := '56789';
OPEN p_ref1 FOR SELECT ROWNUM*2 AS RN FROM DUAL CONNECT BY ROWNUM<=10;
OPEN p_ref2 FOR SELECT ROWNUM*2+1 AS RN FROM DUAL CONNECT BY ROWNUM<=10;

END sp_test;

/

过程已创建。




利用print客户端打印,sqlplus下:

Sql代码
SET AUTOPRINT ON
VAR p_outstr VARCHAR2(10);
VAR p_outint NUMBER;
VAR p_ref1 REFCURSOR;
VAR p_ref2 REFCURSOR;

SET AUTOPRINT ON
VAR p_outstr VARCHAR2(10);
VAR p_outint NUMBER;
VAR p_ref1 REFCURSOR;
VAR p_ref2 REFCURSOR;


Sql代码
scott@ORCL>EXEC sp_test(:p_outstr,:p_outint,:p_ref1,:p_ref2);

PL/SQL 过程已成功完成。


RN
----------
3
5
7
9
11
13
15
17
19
21
23

已选择11行。


RN
----------
2
4
6
8
10
12
14
16
18
20
22

已选择11行。


P_OUTINT
----------
56789


P_OUTSTR
--------------------------------
abc

scott@ORCL>EXEC sp_test(:p_outstr,:p_outint,:p_ref1,:p_ref2);

PL/SQL 过程已成功完成。


RN
----------
3
5
7
9
11
13
15
17
19
21
23

已选择11行。


RN
----------
2
4
6
8
10
12
14
16
18
20
22

已选择11行。


P_OUTINT
----------
56789


P_OUTSTR
--------------------------------
abc 也可以在声明输入输出变量之后,依次print p_outstr,...........

17,091

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧