FUNCTION Fun_Cs_Sum(v_checkid VARCHAR)  RETURN myrctype 
    AS myrctype CURSOR ;
   BEGIN
   OPEN myrctype FOR
     select * from tabel1;       
  RETURN myrctype;
END Fun_Cs_Sum;
报错:
PLS-00320: 此表达式的类型说明不完整或格式错误
说是我的c_cursor没有申明
我看过了溺水三千的例子,他是定义函数在一个package里面,在函数外面申明
type myrctype is ref cursor; 
难道就不能单独使用一个fuction完成返回游标的操作么??
急需高手解答!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

解决方案 »

  1.   

    cursor只是pl/sql语言的变量(对象),并不是oracle数据库的对象,它只能在pl/sql环境中生存。看看下面的例子用函数返回记录集,其中用于存储纪录集的表对象就是oracle的对象(集合类型)--创建测试表:
    SQL> create table a (id number,name varchar2(50),doctime date);Table created.--插入六条测试数据:
    SQL> insert into a values (1,'aaa',to_date('2002-07-01','yyyy-mm-dd'));1 row created.SQL> insert into a values (2,'bbb',to_date('2002-07-02','yyyy-mm-dd'));1 row created.SQL> insert into a values (3,'ccc',to_date('2002-07-03','yyyy-mm-dd'));1 row created.SQL> insert into a values (4,'ddd',to_date('2002-07-04','yyyy-mm-dd'));1 row created.SQL> insert into a values (5,'eee',to_date('2002-07-05','yyyy-mm-dd'));1 row created.SQL> insert into a values (6,'fff',to_date('2002-07-06','yyyy-mm-dd'));1 row created.SQL> commit;Commit complete.--创建两个type
    SQL> create or replace type myobjectype as object (x int,y date,z varchar2(50));
      2  /Type created.SQL> create or replace type mytabletype as table of myobjectype
      2  /Type created.--创建可以返回纪录集的函数(不传入表名参数)
    SQL> create or replace function testrerecordnotabname (tableid in number)
      2  return mytabletype
      3  as
      4    l_data mytabletype :=mytabletype();
      5  begin
      6    for i in (select * from a where id>=tableid)  loop
      7      l_data.extend;
      8      l_data(l_data.count) := myobjectype(i.id,i.doctime,i.name);
      9      exit when i.id = 62;
     10    end loop;  
     11    return l_data;     
     12  end;   
     13  /Function created.SQL> commit;Commit complete.--创建可以返回纪录集的函数(可以传入表名参数)
    SQL> create or replace function testrerecordtabname (tablename in varchar2,tableid in number)
      2  return mytabletype
      3  as
      4    l_data mytabletype :=mytabletype();
      5    strsql varchar2(50);
      6    type v_cursor is ref cursor;
      7    v_tempcursor v_cursor;
      8    i1 number;
      9    i2 varchar2(50);
     10    i3 date;
     11  begin
     12    strsql := 'select * from ' || tablename || ' where id>=' || tableid;
     13    open v_tempcursor for strsql;
     14    loop 
     15      fetch v_tempcursor into i1,i2,i3;
     16      l_data.extend;
     17      l_data(l_data.count) := myobjectype(i1,i3,i2);
     18      exit when v_tempcursor%NOTFOUND;
     19    end loop;  
     20    return l_data;     
     21  end;   
     22  /Function created.SQL> commit;Commit complete.--测试不传表名参数的function(testrerecorenotabname)
    SQL> set serveroutput on     
    SQL> declare
      2    testre mytabletype :=mytabletype();
      3    i number :=0;
      4  begin
      5    testre := testrerecordnotabname(1);
      6    loop
      7      i := i+1;
      8      dbms_output.put_line(';' || testre(i).x || ';' || testre(i).y || ';' || testre(i).z || ';');
      9      exit when i = testre.count;
     10    end loop;
     11  end;
     12  /
    ;1;01-7?? -02;aaa;
    ;2;02-7?? -02;bbb;
    ;3;03-7?? -02;ccc;
    ;4;04-7?? -02;ddd;
    ;5;05-7?? -02;eee;
    ;6;06-7?? -02;fff;PL/SQL procedure successfully completed.--测试传表名参数的function(testrerecoretabname)
    SQL> set serveroutput on     
    SQL> declare
      2    testre mytabletype :=mytabletype();
      3    i number :=0;
      4  begin
      5    testre := testrerecordtabname('a',1);
      6    loop
      7      i := i+1;
      8      dbms_output.put_line(';' || testre(i).x || ';' || testre(i).y || ';' || testre(i).z || ';');
      9      exit when i = testre.count;
     10    end loop;
     11  end;
     12  /
    ;1;01-7?? -02;aaa;
    ;2;02-7?? -02;bbb;
    ;3;03-7?? -02;ccc;
    ;4;04-7?? -02;ddd;
    ;5;05-7?? -02;eee;
    ;6;06-7?? -02;fff;
    ;6;06-7?? -02;fff;PL/SQL procedure successfully completed.
      

  2.   

    你有没有注意到在函数定义部分:
    FUNCTION Fun_Cs_Sum(v_checkid VARCHAR)  RETURN myrctype
    你返回的类型还没有定义过,或者说你返回的类型是在函数体中定义的,ORACLE当然无法识别这样的类型.你可以不把函数写在包体中,但是如果要返回一个纪录集类型的变量,必须先定义REF CURSOR类型的TYPE.