create or replace function getmyanswer4(request varchar2) return varchar2 is
  response varchar2(50);  cursor que is
    select * from question;
  cursor ans is
    select * from answer;  v_que que%rowtype;
  v_ans ans%rowtype;
  
  Deadlock_detected exception;
  Pragma exception_init(Deadlock_detected, -06503);BEGIN
  OPEN que;
  LOOP
    EXIT WHEN que%NOTFOUND;
    fetch que
      into v_que;
  
    IF v_que.q_content = request THEN
      OPEN ans;
      Loop
        EXIT WHEN ans%NOTFOUND;
        fetch ans
          into v_ans;
        if v_ans.q_id = v_que.q_id then
          response := v_ans.a_content;
          return(response);
        end if;
      end loop;
      close ans;
    end if;
  end loop;
  close que;
  exception
      when Deadlock_detected then
      response:='no data found';
      dbms_output.put_line('aaaaaa');
      return (response);
      when others then
      dbms_output.put_line('bbbbbb');
      response:='error';
      return (response);
end;

解决方案 »

  1.   

    EXIT WHEN ans%NOTFOUND;
      fetch ans
      into v_ans;
    这两条语句换一下吧!
      

  2.   

    create or replace function getmyanswer4(request varchar2) return varchar2 is
      response varchar2(50);  cursor que is
      select * from question;
      cursor ans is
      select * from answer;  v_que que%rowtype;
      v_ans ans%rowtype;
       
      Deadlock_detected exception;
      Pragma exception_init(Deadlock_detected, -06503);BEGIN
      OPEN que;
      LOOP
      fetch que  into v_que;
      EXIT WHEN que%NOTFOUND; ----这句放在fetch语句之后   
      IF v_que.q_content = request THEN
      OPEN ans;
      Loop
        fetch ans   into v_ans;
        EXIT WHEN ans%NOTFOUND; ----这句放在fetch语句之后 
      if v_ans.q_id = v_que.q_id then
      response := v_ans.a_content;
      return(response);
      end if;
      end loop;
      close ans;
      end if;
      end loop;
      close que;
      exception
      when Deadlock_detected then
      response:='no data found';
      dbms_output.put_line('aaaaaa');
      return (response);
      when others then
      dbms_output.put_line('bbbbbb');
      response:='error';
      return (response);
    end;
      

  3.   

    不是这个问题,这样是可以执行的
    就是自定义异常上面的问题  函数无返回是OR-06503啊
    搞不懂啊!~
    后来不用这种方式自定义了
    v_exception exception;
    if response is null then
      raise v_exception;
      else
      return ('the answer is  '||response);
    声明了异常变量,然后if判断就可以了
      

  4.   

     EXIT WHEN que%NOTFOUND;
        fetch que
          into v_que;
    应该是这样的啊,不然如果游标一行都没有呢?还去fetch啊?