存储函数的递归调用错误

时间:2018-01-17 13:23:52

标签: oracle

我创建了这个存储函数:

create or replace function recuperer_ascendants_structure(p_struct_code varchar2, previous_codes varchar2)
return varchar2
is
    sep varchar2(1) := '';
    ret varchar2(4000);
    v_ascendant_code structure.str_struct_code%type;
begin
    execute immediate 'select str_struct_code from structure where struct_code = :1' into v_ascendant_code using p_struct_code;
    if v_ascendant_code is null then
        if previous_codes is null then
            return p_struct_code;
        else
            return p_struct_code || ',' || previous_codes;
        end if;
    else
        if previous_codes is null then
            ret := recuperer_ascendants_structure(v_ascendant_code , v_ascendant_code);
        else
            ret := recuperer_ascendants_structure(v_ascendant_code , p_struct_code || ',' || v_ascendant_code);
        end if;
    end if;
end;

在运行时我收到此错误:

SQL> select recuperer_ascendants_structure('21.12.07',null) prts from dual;
select recuperer_ascendants_structure('21.12.07',null) prts from dual
       *
ERROR at line 1:
ORA-06503: PL/SQL: Function returned without value
ORA-06512: at "SSE.RECUPERER_ASCENDANTS_STRUCTURE", line 22
ORA-06512: at "SSE.RECUPERER_ASCENDANTS_STRUCTURE", line 17

那有什么不对?

2 个答案:

答案 0 :(得分:2)

此错误原因可以按原样读取:'返回没有值的函数'。是否意味着执行在没有任何end语句的情况下达到最终return,因此没有任何值可以返回给调用者。

看起来您应该将ret :=替换为return以避免此错误。

答案 1 :(得分:0)

我建议你避免多个RETURN语句。在任何地方收集有关最终结果(代码中的RET变量)的信息,但在函数结束时只返回一次。

CREATE OR REPLACE FUNCTION recuperer_ascendants_structure (
   p_struct_code     VARCHAR2,
   previous_codes    VARCHAR2)
   RETURN VARCHAR2
IS
   sep                VARCHAR2 (1) := '';
   ret                VARCHAR2 (4000);
   v_ascendant_code   structure.str_struct_code%TYPE;
BEGIN
   EXECUTE IMMEDIATE
      'select str_struct_code from structure where struct_code = :1'
      INTO v_ascendant_code
      USING p_struct_code;

   IF v_ascendant_code IS NULL
   THEN
      IF previous_codes IS NULL
      THEN
         ret := p_struct_code;
      ELSE
         ret := p_struct_code || ',' || previous_codes;
      END IF;
   ELSE
      IF previous_codes IS NULL
      THEN
         ret :=
            recuperer_ascendants_structure (v_ascendant_code,
                                            v_ascendant_code);
      ELSE
         ret :=
            recuperer_ascendants_structure (
               v_ascendant_code,
               p_struct_code || ',' || v_ascendant_code);
      END IF;
   END IF;

   RETURN ret;  --> here!
END;