PL/SQL 块问题:未找到数据错误

发布于 2024-08-02 06:52:31 字数 1348 浏览 1 评论 0原文

SET SERVEROUTPUT ON
DECLARE
    v_student_id NUMBER := &sv_student_id;
    v_section_id NUMBER := 89;
    v_final_grade NUMBER;
    v_letter_grade CHAR(1);
BEGIN
    SELECT final_grade
    INTO v_final_grade
    FROM enrollment
    WHERE student_id = v_student_id
    AND section_id = v_section_id;

    CASE -- outer CASE
        WHEN v_final_grade IS NULL THEN
            DBMS_OUTPUT.PUT_LINE ('There is no final grade.');
        ELSE
            CASE -- inner CASE
                WHEN v_final_grade >= 90 THEN v_letter_grade := 'A';
                WHEN v_final_grade >= 80 THEN v_letter_grade := 'B';
                WHEN v_final_grade >= 70 THEN v_letter_grade := 'C';
                WHEN v_final_grade >= 60 THEN v_letter_grade := 'D';
                ELSE v_letter_grade := 'F';
            END CASE;

            -- control resumes here after inner CASE terminates
            DBMS_OUTPUT.PUT_LINE ('Letter grade is: '||v_letter_grade);
    END CASE;
    -- control resumes here after outer CASE terminates
END;

上面的代码是我从《Oracle PL/SQL by Examples,2009 年第 4 版》一书中获取的,我的问题是当我输入一个不存在于它返回的表中的 student_id 时我遇到以下错误

Error report: ORA-01403: no data found
ORA-06512: at line 7
01403. 00000 -  "no data found"
*Cause:    
*Action:

,但根据这本书,它应该返回一个空值,然后按照案例流程进行操作。

SET SERVEROUTPUT ON
DECLARE
    v_student_id NUMBER := &sv_student_id;
    v_section_id NUMBER := 89;
    v_final_grade NUMBER;
    v_letter_grade CHAR(1);
BEGIN
    SELECT final_grade
    INTO v_final_grade
    FROM enrollment
    WHERE student_id = v_student_id
    AND section_id = v_section_id;

    CASE -- outer CASE
        WHEN v_final_grade IS NULL THEN
            DBMS_OUTPUT.PUT_LINE ('There is no final grade.');
        ELSE
            CASE -- inner CASE
                WHEN v_final_grade >= 90 THEN v_letter_grade := 'A';
                WHEN v_final_grade >= 80 THEN v_letter_grade := 'B';
                WHEN v_final_grade >= 70 THEN v_letter_grade := 'C';
                WHEN v_final_grade >= 60 THEN v_letter_grade := 'D';
                ELSE v_letter_grade := 'F';
            END CASE;

            -- control resumes here after inner CASE terminates
            DBMS_OUTPUT.PUT_LINE ('Letter grade is: '||v_letter_grade);
    END CASE;
    -- control resumes here after outer CASE terminates
END;

the above code I have taken from the book "Oracle PL/SQL by Example, 4th Edition 2009" my problem is when I enter a student_id not present in the table it returns me the following error

Error report: ORA-01403: no data found
ORA-06512: at line 7
01403. 00000 -  "no data found"
*Cause:    
*Action:

but according to the book it should have returned a null value and then follow the case flow.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

陈年往事 2024-08-09 06:52:31

当您选择 INTO 变量并且没有返回记录时,您应该收到 NO DATA FOUND 错误。我相信编写上述代码的正确方法是将 SELECT 语句用它自己的 BEGIN/EXCEPTION/END 块包装。例子:

...
v_final_grade NUMBER;
v_letter_grade CHAR(1);
BEGIN

    BEGIN
    SELECT final_grade
      INTO v_final_grade
      FROM enrollment
     WHERE student_id = v_student_id
       AND section_id = v_section_id;

    EXCEPTION
      WHEN NO_DATA_FOUND THEN
        v_final_grade := NULL;
    END;

    CASE -- outer CASE
      WHEN v_final_grade IS NULL THEN
      ...

When you are selecting INTO a variable and there are no records returned you should get a NO DATA FOUND error. I believe the correct way to write the above code would be to wrap the SELECT statement with it's own BEGIN/EXCEPTION/END block. Example:

...
v_final_grade NUMBER;
v_letter_grade CHAR(1);
BEGIN

    BEGIN
    SELECT final_grade
      INTO v_final_grade
      FROM enrollment
     WHERE student_id = v_student_id
       AND section_id = v_section_id;

    EXCEPTION
      WHEN NO_DATA_FOUND THEN
        v_final_grade := NULL;
    END;

    CASE -- outer CASE
      WHEN v_final_grade IS NULL THEN
      ...
醉态萌生 2024-08-09 06:52:31

当我无法依赖过程底部的 EXCEPTION 块时,我使用了另一种方法。我在开始时声明了变量:

my_value VARCHAR := 'default';
number_rows NUMBER := 0;
.
.
.
SELECT count(*) FROM TABLE INTO number_rows (etc.)

IF number_rows > 0 -- Then obtain my_value with a query or constant, etc.
END IF;

There is an alternative approach I used when I couldn't rely on the EXCEPTION block at the bottom of my procedure. I had variables declared at the beginning:

my_value VARCHAR := 'default';
number_rows NUMBER := 0;
.
.
.
SELECT count(*) FROM TABLE INTO number_rows (etc.)

IF number_rows > 0 -- Then obtain my_value with a query or constant, etc.
END IF;
晨曦÷微暖 2024-08-09 06:52:31

可能值得在线检查您的书的勘误部分。

这里有一个处理此异常的示例 http://www.dba-oracle.com/sf_ora_01403_no_data_found。嗯

Might be worth checking online for the errata section for your book.

There's an example of handling this exception here http://www.dba-oracle.com/sf_ora_01403_no_data_found.htm

不…忘初心 2024-08-09 06:52:31

您的 SELECT 语句未找到您要查找的数据。也就是说,ENROLLMENT 表中没有具有给定 STUDENT_IDSECTION_ID 的记录。您可能需要在运行查询之前尝试放置一些 DBMS_OUTPUT.PUT_LINE 语句,打印 v_student_idv_section_id 的值。它们可能不包含您期望的内容。

Your SELECT statement isn't finding the data you're looking for. That is, there is no record in the ENROLLMENT table with the given STUDENT_ID and SECTION_ID. You may want to try putting some DBMS_OUTPUT.PUT_LINE statements before you run the query, printing the values of v_student_id and v_section_id. They may not be containing what you expect them to contain.

冰雪之触 2024-08-09 06:52:31

由于我们正在使用某些数据类型,因此未找到此数据。

就像上面的 select empid into v_test

empid 并且 v_test 必须是 number 类型,那么只会存储数据。

因此,当收到此错误时,请跟踪数据类型,这可能会有所帮助

This data not found causes because of some datatype we are using .

like select empid into v_test

above empid and v_test has to be number type , then only the data will be stored .

So keep track of the data type , when getting this error , may be this will help

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文