create or replace type test_type as varray(20) of varchar2(10);  --定义一个arraycreate or replace function travel(array test_type) return varchar2   -- 遍历array的方法
is
  len number;
  i number;
begin
  len := array.count();
  for i in 1..len Loop
    dbms_output.put_line(array(i));
  end Loop;
end;declare
  array1 array_type := array_type('a','b');
begin
  travel(array1); --直接报错
end;
错误信息:
PLS-00221: 'TRAVEL' is not a procedure or is undefined
ORA-06550: line 4, column 3:
非常奇怪,上面的定义全部成功了,但是吊用的时候就报undefined,难道是参数是自定义的原因么,请赐教。

解决方案 »

  1.   

    create or replace procedure travel(array test_type)
    is
      len number;
      i number;
    begin
      len := array.count();
      for i in 1..len Loop
        dbms_output.put_line(array(i));
      end Loop;
    end;declare
      array1 test_type:= test_type('a','b');
    begin
     travel(array1) ; --直接报错
    end;
      

  2.   


    你看错误信息:
    PLS-00221: 'TRAVEL' is not a procedure or is undefined
    就知道了撒?
    travel要改成存储过程
    另外:你函数有返回值?都是null吧,函数体里没见哪里返回了varchar2型的
      

  3.   

    函数是一定要有return的
    而你定义的函数没有,所有你的应该是像1楼兄弟说的那样用存储过程
      

  4.   


    OK,我以为我不需要返回值就不用写return 了呢
      

  5.   

    我现在把这个function改成procedure就OK了,但是我就是想用function来做这个功能难道就不行了,我在function里面加了return也不行,该怎么做。
      

  6.   

    SQL> create or replace type test_type as varray(20) of varchar2(10); 
      2  /
     
    Type created
     
    SQL> 
    SQL> create or replace function travel(array test_type) return varchar2   -- 遍历array的方法
      2  is
      3    len number;
      4    i number;
      5    v_re varchar2(4000);
      6  begin
      7    len := array.count();
      8    v_re:=null;
      9    for i in 1..len Loop
     10      dbms_output.put_line(array(i));
     11      v_re:=v_re||array(i);
     12    end Loop;
     13    return v_re;
     14  end;
     15  /
     
    Function created
     
    SQL> set serveroutput on
    SQL> 
    SQL> declare
      2    array1 test_type := test_type('a','b');
      3    v_c  varchar2(4000);
      4  begin
      5    v_c:=travel(array1); --直接报错
      6    dbms_output.put_line(v_c);
      7  end;
      8  /
     
    a
    b
    ab
     
    PL/SQL procedure successfully completed
     
    SQL> 
      

  7.   


    果然OK了function里面加了return,在procedure里面这样用 str := travel(array); 就没有问题了刚才搜索了一下procedure和function的区别,好像说function必须写在一个表达式里面,照这个例子来看好像就是的,function不能单独直接使用,procedure肯定可以的,这就是两者的区别么?
      

  8.   

    恩,可以这么说.函数如果只有一个返回值,没有返回参数的时候可以用在sql中,procedure是不能用在sql中的.另外就是你刚才说的,函数不能单独调用,过程可以.
      

  9.   

    函数如果只有一个返回值,没有返回参数的时候可以用在sql中关于这个能在说详细一些么,我现在有点糊昨天分就全给你了,今天估计也是你的了,呵呵
      

  10.   

    呵呵,别都给我,大家也付出劳动了.
    函数也可以写out参数的,但是有out参数的函数就不能在sql语句里直接调用了.