ORA-06503:PL / SQL:返回的函数没有值,第21行ORA-06512:在第5行

时间:2018-09-29 03:59:54

标签: oracle plsql oracle11g

    create or replace FUNCTION NOTGIVEN RETURN VARCHAR2 AS
    IAD VARCHAR2(20);
    IND VARCHAR2(20);
    CURSOR C1 IS SELECT Q_ID  FROM QUESTION  WHERE NOT Q_ID in (SELECT Q.Q_ID FROM QUESTION Q INNER JOIN ANSWER A ON Q.Q_ID = A.Q_ID); 

    BEGIN
     OPEN C1;
      LOOP
      FETCH C1 INTO IND;
      EXIT WHEN C1%NOTFOUND;

      IF C1%FOUND THEN --if found then
      SELECT Q_ID INTO IAD FROM QUESTION WHERE Q_ID = IND;
      DBMS_OUTPUT.PUT_LINE(IAD);
      END IF;
     IF C1%NOTFOUND THEN
      DBMS_OUTPUT.PUT_LINE('no found');--if not found
      END IF;
     END LOOP;
     CLOSE C1;
      END NOTGIVEN;

我试图使函数给出没有答案的所有问题,它给出适当的输出,但显示这种类型的错误ORA-06503:PL / SQL:函数无值返回,第21行ORA-06512:在5岁时,我也尝试了异常,但没有用。

1 个答案:

答案 0 :(得分:0)

您已编写了一个应该返回VARCHAR2值的函数,但是代码中没有return语句,因此例程不返回值。

CREATE OR REPLACE FUNCTION NOTGIVEN
  RETURN VARCHAR2
AS
  IAD VARCHAR2(20);
  IND VARCHAR2(20);

  CURSOR C1 IS SELECT Q_ID
                 FROM QUESTION
                 WHERE NOT Q_ID IN (SELECT Q.Q_ID
                                      FROM QUESTION Q
                                      INNER JOIN ANSWER A
                                        ON Q.Q_ID = A.Q_ID); 
BEGIN
  OPEN C1;

  LOOP
    FETCH C1 INTO IND;
    EXIT WHEN C1%NOTFOUND;

    IF C1%FOUND THEN --if found then
      SELECT Q_ID
        INTO IAD
        FROM QUESTION
        WHERE Q_ID = IND;

      DBMS_OUTPUT.PUT_LINE(IAD);
    END IF;

    IF C1%NOTFOUND THEN
      DBMS_OUTPUT.PUT_LINE('no found');--if not found
    END IF;
  END LOOP;

  CLOSE C1;

  RETURN IAD;  -- added
END NOTGIVEN;

在这里,我添加了一条语句,在IAD之前返回END NOTGIVEN

好运。