Oracle SYS_REFCURSOR 不能用作返回类型

Oracle SYS_REFCURSOR couldn#39;t use as a return type(Oracle SYS_REFCURSOR 不能用作返回类型)
本文介绍了Oracle SYS_REFCURSOR 不能用作返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 oracle 11g 中的成员函数提取并显示 db 中所有记录的所有年份.

I need to extract and display all the years for all the records in db using member function in oracle 11g.

CREATE or replace TYPE BODY student_t AS 
MEMBER FUNCTION getYear RETURN SYS_REFCURSOR IS
yearDOB SYS_REFCURSOR;
BEGIN
    for c in (SELECT EXTRACT(YEAR FROM s.dob) c_year from student s)
    loop
        yearDOB := c.c_year; 
    end loop;
    return yearDOB; 
END;END;/

因为我需要为提取函数返回多个值,所以我声明了一个要返回的 SYS_REFCURSOR 类型变量.但是会产生如下错误.

Since I need to return multiple values for the extract function I have declared a SYS_REFCURSOR type variable to return. But it will generate following errors.

7/1      PL/SQL: Statement ignored,
7/14     PLS-00382: expression is of wrong type

更改后的输出如下答案.

output after changed as following answer.

光标语句:1

光标语句:1

  1993
  1995

光标语句:1

光标语句:1

  1993
  1995

学生类型如下.

create type student_t as object(
stno char(4),
dob date)/ 
alter type student_t add member function getYear RETURN SYS_REFCURSOR cascade;/

推荐答案

我不确定您要实现的目标,但我认为您误解了 oracle 中对象的概念.在我的例子中,我假设
1) 表studens是用这个脚本创建的create table student of student_t;

I'm not sure what you are trying to achieve but I think you misunderstood concept of object in oracle. In my example i'm assuming
1) Table studens is create with this script create table student of student_t;

drop table student;

创建类型规范和正文.

create or replace type student_t as object(
stno char(4),
dob date,
member function getYear return number

)
;

create or replace type body student_t as

    member function getYear return number is 
    begin 
            return  EXTRACT(YEAR FROM self.dob);
    end;

end;

创建studnet表

create table student of student_t;

填充表格

declare 
  v_student student_t;
begin 
for i in 0 .. 10 loop 
  insert into student values(student_t('ST'||+mod(i,3),to_date('01-01-'||to_char(2000+i),'dd-mm-yyyy')));
end loop;
commit;
end; 

还有查询.

select s.*,s.getYear() from student s;

这篇关于Oracle SYS_REFCURSOR 不能用作返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

SQL to Generate Periodic Snapshots from Transactions Table(用于从事务表生成定期快照的SQL)
MyBatis support for multiple databases(MyBatis支持多个数据库)
Oracle 12c SQL: Missing column Headers in result(Oracle 12c SQL:结果中缺少列标题)
SQL query to find the number of customers who shopped for 3 consecutive days in month of January 2020(查询2020年1月连续购物3天的客户数量)
How to get top 10 data weekly (This week, Previous week, Last month, 2 months ago, 3 month ago)(如何每周获取前十大数据(本周、前一周、上个月、2个月前、3个月前))
Select the latest record for an Id per day - Oracle pl sql(选择每天ID的最新记录-Oracle pl SQL)