我需要使用oracle 11g中的成员函数提取并显示db中所有记录的所有年份。
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;/
由于我需要为extract函数返回多个值,因此我已声明要返回的SYS_REFCURSOR类型变量。但它会产生以下错误。
7/1 PL/SQL: Statement ignored,
7/14 PLS-00382: expression is of wrong type
输出后更改为以下答案。
CURSOR声明:1
CURSOR声明:1
1993
1995
CURSOR声明:1
CURSOR声明: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;/
答案 0 :(得分:2)
我不确定你想要实现什么,但我认为你在oracle中误解了对象的概念。
在我的例子中,我假设是
1)使用此脚本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;
答案 1 :(得分:1)
更简单:
yearDOB SYS_REFCURSOR;
BEGIN
OPEN yearDOB for
SELECT EXTRACT(YEAR FROM s.dob) c_year
from student s;
return yearDOB;
END;