Oracle PL/SQL游标中的简单常见错误ORA-06550
这是一个简单的PL/SQL块,我有一个非常简单的错误,ORA-06550,但我找不到它,请帮我解决这个错误 问题:按类别显示最昂贵物品的奖品 create table menu ( item_id number primary key, name_ varchar2(20), prize number, category varchar(15) ); create table order_ ( o_id number primary key, item_id number references menu(item_id), table_no number, qty number ); 错误已被清除Oracle PL/SQL游标中的简单常见错误ORA-06550,oracle,plsql,database-cursor,ora-06550,Oracle,Plsql,Database Cursor,Ora 06550,这是一个简单的PL/SQL块,我有一个非常简单的错误,ORA-06550,但我找不到它,请帮我解决这个错误 问题:按类别显示最昂贵物品的奖品 create table menu ( item_id number primary key, name_ varchar2(20), prize number, category varchar(15) ); create table order_ ( o_id number primary key, it
ERROR at line 6:
ORA-06550: line 6, column 40:
PLS-00103: Encountered the symbol "(" when expecting one of the following:
:= . ) , @ % default character
The symbol ":=" was substituted for "(" to continue.
ORA-06550: line 25, column 20:
PLS-00103: Encountered the symbol "(" when expecting one of the following:
. into bulk
ORA-06550: line 25, column 80:
PLS-00103: Encountered the symbol ";" when expecting one of the following:
. ( , % from
ORA-06550: line 29, column 24:
PLS-00103: Encountered the symbol "(" when expecting one of the following:
. into bulk
ORA-06550: line 29, column 84:
PLS-00103: Encountered the symbol ";" when expecting one of the following:
. ( , % from
ORA-06550: line 35, column 5:
PLS-00103: Encountered the symbol "CLOSE" when expecting one of the following:
end not pragma final instantiable order overriding static
member constructor map
所以你的匿名街区太乱了。ORA-06550不是您的问题,PLS-00103是,它与ORA-06550一起标志着您将代码弄乱的地方 首先,游标声明不符合中的语法方案<代码>光标数据2(pr编号,cat varchar(15))应该是
光标数据2(pr编号,cat varchar)
,请注意您的第一个错误是如何显示ORA-06550:第6行,第40列:PLS-00103:遇到符号“(,当需要下列之一时:=),@%默认字符
,因为在第6行它遇到了(
作为varchar(15)
的一部分,它不应该在那里
您的游标处理到处都是。游标参数是在打开游标时提供的,而不是在获取游标时提供的,而且您在循环外部打开和关闭游标data2
,这意味着在获取时循环中的数据不会更改。正如我前面提到的,获取语法完全错误,没有提供任何参数在获取数据时,将指针指向游标。这一切都可以修复,但我想提出一种更清晰、更简单的方法来调用游标(除非您必须在赋值中使用open、fetch、close):
下面是您的原始代码的固定游标用法,我建议您阅读更多有关游标的内容。例如,我添加了注释以更好地了解代码中发生了什么:
declare
cursor data1 is select max(prize) as "prize_", category
from menu
group by category;
cursor data2(pr number, cat varchar) is
select prize, name_, category
from menu
where prize = pr and category = cat;
data1_cat varchar(15);
data1_pri number;
data2_cat varchar(15);
data2_pri number;
data2_name varchar(15);
begin
-- Open cursor data1 and start looping over data in cursor
open data1;
loop
-- Fetch cursor data1 values into variables
fetch data1 into data1_pri, data1_cat;
-- Check if cursor provided data, if not exit loop
exit when data1%notfound;
-- Open cursor data2 with parameters from variables filled with data from cursor data1 and start looping over data in cursor
open data2(data1_pri, data1_cat);
loop
-- Fetch cursor data2 values into variables
fetch data2 into data2_pri, data2_name, data2_cat;
-- Check if cursor provided data, if not exit loop
exit when data2%notfound;
-- Handle variables as needed
dbms_output.put_line(data2_name || ' ' || data2_pri || ' ' || data2_cat);
end loop;
-- Close cursor data2 as new cursor will be opened in next loop and without closing it would lead to ORA-06511: PL/SQL: cursor already open
close data2;
end loop;
-- Close cursor data1 as good practice and possibly to avoid ORA-01000: maximum open cursors exceeded
close data1;
end;
/
请详细介绍如何使用循环在显式游标上迭代。您的代码不正确。对于您将来的问题,我建议您阅读本文,它可以减少您的问题被否决的机会。另外,示例数据也将非常感谢。您能提供一些最佳资源来学习和实践SQL和PL/SQL吗?请查看此answer:,它有多个好答案,有好的来源。
declare
cursor data1 is select max(prize) as "prize_", category
from menu
group by category;
cursor data2(pr number, cat varchar) is
select prize, name_, category
from menu
where prize = pr and category = cat;
begin
for c in data1 loop
for i in data2(c."prize_", c.category) loop
dbms_output.put_line(i.name_ || ' ' || i.prize || ' ' || i.category);
end loop;
end loop;
end;
/
declare
cursor data1 is select max(prize) as "prize_", category
from menu
group by category;
cursor data2(pr number, cat varchar) is
select prize, name_, category
from menu
where prize = pr and category = cat;
data1_cat varchar(15);
data1_pri number;
data2_cat varchar(15);
data2_pri number;
data2_name varchar(15);
begin
-- Open cursor data1 and start looping over data in cursor
open data1;
loop
-- Fetch cursor data1 values into variables
fetch data1 into data1_pri, data1_cat;
-- Check if cursor provided data, if not exit loop
exit when data1%notfound;
-- Open cursor data2 with parameters from variables filled with data from cursor data1 and start looping over data in cursor
open data2(data1_pri, data1_cat);
loop
-- Fetch cursor data2 values into variables
fetch data2 into data2_pri, data2_name, data2_cat;
-- Check if cursor provided data, if not exit loop
exit when data2%notfound;
-- Handle variables as needed
dbms_output.put_line(data2_name || ' ' || data2_pri || ' ' || data2_cat);
end loop;
-- Close cursor data2 as new cursor will be opened in next loop and without closing it would lead to ORA-06511: PL/SQL: cursor already open
close data2;
end loop;
-- Close cursor data1 as good practice and possibly to avoid ORA-01000: maximum open cursors exceeded
close data1;
end;
/