在 pl/sql 过程中出现错误以查找 topper 的名称

Posted

技术标签:

【中文标题】在 pl/sql 过程中出现错误以查找 topper 的名称【英文标题】:getting an error in the pl/sql procedure for finding the name of the topper 【发布时间】:2017-10-23 14:41:15 【问题描述】:

这是程序:

ERROR 1064 (42000):您的 SQL 语法有错误;检查与您的 mysql 服务器版本相对应的手册,以了解在 'procedure topperstudent 附近使用的正确语法 作为 topper students.name%type' 在第 1 行

create or replace procedure topperstudent
as
topper students.name%type;
begin
select name into topper from students where marks = (select max(marks) from 
students)enter code here
dbms_output.put_line(topper);
end;
/

【问题讨论】:

您使用的是 MySQL 还是 Oracle? 您的错误消息表明您使用的是 MySQL 而不是 PL/SQL(已标记),在这种情况下,如果 PL/SQL Developer(也已标记)启动,我会感到非常惊讶。 create or replace procedure%type 是 Oracle 语法,而不是 MySQL。 【参考方案1】:

如果有多个学生获得最高分,您的代码将通过异常。

下面的代码可以试试。

create or replace procedure topperstudent
as
topper students.name%type;
begin
dbms_output.put_line('topper Names :');
for Student_name in (select name from students where marks = (select max(marks) from 
students))
loop
dbms_output.put_line(Student_name.name);
end loop;
end;

以上代码假设我们在学生表中至少有一个学生数据。

否则,我们可以对学生表中的学生计数进行验证检查,并根据计数 > 0 执行 for 循环。

【讨论】:

在topper students.name%type 之后给了我这个错误; ERROR 1064 (42000):您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以了解在 'procedure topperstudent 附近使用的正确语法 只有一个最高分的条目,仍然报错 尝试使用 varchar(50) 代替 students.name%type 并检查;这个上面的答案我已经提到假设 PLSQL。

以上是关于在 pl/sql 过程中出现错误以查找 topper 的名称的主要内容,如果未能解决你的问题,请参考以下文章

如何修改我的 PL/SQL 过程以进行异常处理?

创建 pl/sql 过程以从不同的表中获取不同的列

执行函数时 PL/SQL Oracle Package 错误

我的 VB.Net 代码中出现 PL SQL 错误,但无法在 SQL Developer 中重现

pl/sql 中带参数的过程

执行存储过程 - Oracle PL SQL