提问人:Fayzie 提问时间:5/24/2022 最后编辑:LittlefootFayzie 更新时间:5/24/2022 访问量:57
尝试声明游标时出现此错误
Got this error when trying to declare a cursor
答:
1赞
Littlefoot
5/24/2022
#1
- %吵吵嚷嚷 Be?
fetch
应以分号结尾when
子句没有=
exit
子句应紧随其后fetch
因此,对于示例数据
SQL> SELECT * FROM countries;
COUNTRY_ID COUNTRY REGION_ID
---------- ------- ----------
1 Croatia 1
2 Austria 1
3 Germany 2
SQL> SET SERVEROUTPUT ON
该 PL/SQL 块可能如下所示:
SQL> DECLARE
2 CURSOR cur_country IS
3 SELECT country_id, country_name, region_id
4 FROM countries
5 WHERE region_id = 1;
6
7 v_ctr_record cur_country%ROWTYPE;
8 BEGIN
9 OPEN cur_country;
10
11 LOOP
12 FETCH cur_country INTO v_ctr_record;
13
14 EXIT WHEN cur_country%NOTFOUND;
15
16 DBMS_OUTPUT.put_line (
17 'Country name '
18 || v_ctr_record.country_name
19 || ' country_id '
20 || v_ctr_record.country_id
21 || ' Region id '
22 || v_ctr_record.region_id);
23 END LOOP;
24
25 CLOSE cur_country;
26 END;
27 /
Country name Croatia country_id 1 Region id 1
Country name Austria country_id 2 Region id 1
PL/SQL procedure successfully completed.
SQL>
或者,切换到光标循环;使用起来更简单:FOR
SQL> BEGIN
2 FOR v_ctr_record IN (SELECT country_id, country_name, region_id
3 FROM countries
4 WHERE region_id = 1)
5 LOOP
6 DBMS_OUTPUT.put_line (
7 'Country name '
8 || v_ctr_record.country_name
9 || ' country_id '
10 || v_ctr_record.country_id
11 || ' Region id '
12 || v_ctr_record.region_id);
13 END LOOP;
14 END;
15 /
Country name Croatia country_id 1 Region id 1
Country name Austria country_id 2 Region id 1
PL/SQL procedure successfully completed.
SQL>
评论