gpt4 book ai didi

sql - PL/SQL block 问题 : No data found error

转载 作者:行者123 更新时间:2023-12-03 06:12:43 25 4
gpt4 key购买 nike

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 示例,2009 年第 4 版》一书中获取的,我的问题是当我输入的 student_id 中不存在时表它返回给我以下错误

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

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

最佳答案

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

...
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
...

关于sql - PL/SQL block 问题 : No data found error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1256112/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com