我一直在尝试运行这个脚本,但我得到的只是这个错误:
Error report:
ORA-06550: line 3, column 15:
PLS-00103: Encountered the symbol "IS" when expecting one of the following:
constant exception <an identifier>
<a double-quoted delimited-identifier> table long double ref
char time timestamp interval date binary national character
nchar
The symbol "IS" was ignored.
这是我的剧本:
set serveroutput on
DECLARE
cursor depts_cur is select dname from dept;
depts_cur_rec is depts_cur%type;
BEGIN
loop
fetch depts_cur into depts_cur_rec;
exit when depts_cur_rec%notfound;
dbms_output.put_line('Department: ' || depts_cur_rec);
end loop;
close depts_cur;
END;
非常感谢您的帮助。
答案 0 :(得分:6)
看起来你的depts_cur_rec声明是错误的,试试这个(删除“是”):
set serveroutput on
DECLARE
cursor depts_cur is select dname from dept;
depts_cur_rec depts_cur%type;
BEGIN
BEGIN loop
fetch depts_cur
into depts_cur_rec;
exit when depts_cur_rec%notfound;
dbms_output.put_line('Department: ' || depts_cur_rec);
end loop;
close depts_cur;
END;
答案 1 :(得分:1)
首先,当您声明像depts_cur_rec
这样的局部变量时,不要使用IS
关键字。并且您希望depts_cur_rec
被声明为%ROWTYPE
,而不是%TYPE
。一旦超过它,您的代码就会失败,因为您在获取之前没有打开游标,并且因为您尝试使用记录的%NOTFOUND
属性而不是光标。最后,您的DBMS_OUTPUT
调用需要引用记录中的特定列
DECLARE
cursor depts_cur is select dname from dept;
depts_cur_rec depts_cur%rowtype;
BEGIN
open depts_cur;
loop
fetch depts_cur into depts_cur_rec;
exit when depts_cur%notfound;
dbms_output.put_line('Department: ' || depts_cur_rec.dname);
end loop;
close depts_cur;
END;
然而,对于这种事情,简单地使用隐式游标而不是显式游标可能要容易得多。这样,您无需手动指定OPEN
,FETCH
或CLOSE
。无需手动确定何时退出循环。无需手动声明记录类型。你让Oracle处理那个
BEGIN
FOR dept_rec IN (SELECT dname
FROM dept)
LOOP
dbms_output.put_line( 'Department: ' || dept_rec.dname );
END LOOP;
END;
如果要将光标声明为不符合行,则可以使用隐式游标执行此操作
DECLARE
CURSOR depts_cur
IS SELECT dname
FROM dept;
BEGIN
FOR dept_rec IN depts_cur
LOOP
dbms_output.put_line( 'Department: ' || dept_rec.dname );
END LOOP;
END;