奇怪的错误,导致我的脚本无法运行

Posted

技术标签:

【中文标题】奇怪的错误,导致我的脚本无法运行【英文标题】:weird error, causing my script not to run 【发布时间】:2020-10-01 01:50:12 【问题描述】:

我正在尝试运行我在下面附加的 pl/sql 过程,并且在运行我的脚本时遇到以下错误

DBMS_OUTPUT.ENABLE;

create or replace procedure grade_point212
is
cursor c1 is
----Cursor declaration--
SELECT student.student_id, student.first_name, student.last_name, course.course_id, course.course_name, class.grade
FROM CLASS
JOIN STUDENT
  ON student.student_id = class.student_id
  JOIN course
  ON course.course_id = class.course_id
order by 1;
----Variable declation--
v_student_id student.student_id%type;
v_first_name  student.first_name%type;
v_last_name student.last_name%type;
v_course_id course.course_id%type;
v_course_name course.course_name%type;
v_grade class.grade%type;
--3 additional varriables--
prev_student student.student_id%type;
course_count  number(3);
grade_point_total  number(3);
----mainline--
begin
    open c1; 
    loop
--Get the first row in the cursor--
    fetch c1 
    into v_student_id,v_first_name ,v_last_name,v_course_id,v_course_name,v_grade;
    exit when c1%notfound;
--Set the prev_student to the cursor’s student id--
        prev_student:=v_student_id;
--Set the grade_point _total to 0--
        grade_point_total:=0;
--Set the course_count to 0--
        course_count:=0;
--If the prev_studentis NOT equal to cursor’s student id--
            IF prev_student!=v_student_id  THEN
--Print out the grade point average which is grade_point_total divided by course_count--
                DBMS_OUTPUT.PUT_LINE(grade_point_total/course_count); 
--Set prev_student to the cursor’s student id--
                prev_student:=v_student_id;
--Set the grade_point_total to 0--
                grade_point_total:=0;
--Set the course_count to 0--
                course_count:=0;
            END IF;
--Add the grade point of the cursor’s grade to grade_point_total--
grade_point_total:=grade_point_total+GradePoint(v_grade);
--Add 1 to the course_count--
course_count:=course_count+1;
--Print out the current row--
 DBMS_OUTPUT.PUT_LINE(v_student_id||' '||v_first_name||' '||v_last_name||' '||v_course_id||' '||v_course_name||' '||v_grade); 
--Fetch a new row--
 fetch c1 
 into v_student_id,v_first_name ,v_last_name,v_course_id,v_course_name,v_grade;
end loop; 
--Close the cursor--
close c1; 
--Print out the grade point average which is grade_point_total divided by course_count--
DBMS_OUTPUT.PUT_LINE(grade_point_total/course_count);
end;


set serveroutput on;
begin
 grade_point212;
end;
“从第 1 行开始的错误命令 - DBMS_OUTPUT.ENABLE 错误报告 - 未知的命令 程序 GRADE_POINT212 已编译 LINE/COL 错误 --------- ----------------------------------------- -------------------- 65/1 PLS-00103:遇到符号“SET” 错误:检查编译器日志 "

【问题讨论】:

你想在哪里执行这些语句?在 SQL 命令提示符或任何客户端工具中? 【参考方案1】:

“/”是正确的。作为跟进,您使用什么工具将脚本发送到数据库?

SQL*Plus 使用独立的“/”行作为字符串序列完整的指示符,因此可以发送到数据库进行编译和执行。如果您不提供“/”,您将收到错误,仔细阅读会表明某些编译已失败,因为您发送的序列实际上将由多个 SQL、SQL*Plus 和 PLSQL 块组成。

execute 关键字,顺便说一句,如:

执行 dbms_output.enable;

是 SQLPlus syntactic sugar 被 SQLPlus 转换为:

开始 dbms_output.enable;结束;

【讨论】:

【参考方案2】:

在你的 dbms 前尝试 exec..

执行 dbms_output.enable;

【讨论】:

doesnt work 我得到同样的错误,我想我可能有语法错误。我正在检查它,看看我是否能解决这个问题。【参考方案3】:

完全删除第一行 (DBMS_OUTPUT.ENABLE;)。 set serveroutput on(在您即将执行该过程之前)将启用它。

或者,将其封装到过程或匿名 PL/SQL 块本身中,例如

SQL> begin
  2    dbms_output.enable;
  3  end;
  4  /

PL/SQL procedure successfully completed.

SQL>

【讨论】:

【参考方案4】:

您错过了结尾的“/”来执行“CREATE OR REPLACE”语句。这 ”;”正如here 解释的那样,分号结束您的 pl/sql 语句,但您需要斜杠来执行它。

错误“遇到符号“SET”错误:检查编译器日志”指的是“set serveroutput on”语句中的“SET”字符串。

...
DBMS_OUTPUT.PUT_LINE(grade_point_total/course_count);
end;
/  <<< Add this 

set serveroutput on;
begin
 grade_point212;
end;
/  <<< Add this

【讨论】:

以上是关于奇怪的错误,导致我的脚本无法运行的主要内容,如果未能解决你的问题,请参考以下文章

奇怪和疯狂的 PHP 错误,清除浏览器历史记录后的页面加载导致脚本多次运行 [重复]

我无法直接运行python脚本的原因是什么?

dos下编辑过sh脚本后无法运行

inotify为啥无法检视共享文件夹

Intellij/Pycharm 无法调试 Python 模块

PHP argv丢失导致脚本无法运行[重复]