SQL> create or replace procedure p_demo_cr_read_change is
2 cursor c1 is select * from emp where empno=7369;
3 employee_rec emp%rowtype;
4 begin
5 open c1;
6 dbms_lock.sleep(60);
7
8 fetch c1 into employee_rec;
9 while (c1%found) loop
10
11 dbms_output.put_line(‘employee id: ‘ || employee_rec.empno);
12 dbms_output.put_line(‘employee name: ‘ || employee_rec.ename);
13
14 fetch c1 into employee_rec;
15 end loop;
16 close c1;
17 end p_demo_cr_read_change;
18 /
以上这段存储过程,可以解释下emp%rowtype的含义么?为什么我改为:
SQL> create or replace procedure p_demo_cr_read_change is
2 cursor c1 is select object_name from dba_objects where object_id=20;
3 employee_rec dba_objects%rowtype;
4 begin
5 open c1;
6 dbms_lock.sleep(60);
7
8 fetch c1 into employee_rec;
9 while (c1%found) loop
10 dbms_output.put_line('employee id: ' || employee_rec.object_id);
11 dbms_output.put_line('employee name: ' || employee_rec.object_name);
12
13 fetch c1 into employee_rec;
14 end loop;
15 close c1;
16 end p_demo_cr_read_change;
17 /
提示Warning: Procedure created with compilation errors.呢?谢谢!
------解决思路----------------------
select * from emp ==> emp%rowtype
select object_name from dba_objects ==> varchar2
------解决思路----------------------
emp%rowtype 表示数据表emp的行类型;
而你的游标cursor c1 is select object_name from dba_objects where object_id=20只是表示是一个字符串类型,两个类型不一致,自然报错。
------解决思路----------------------
可以这么理解
emp%rowtype可以存储emp表的一整行数据;
因为
cursor c1 is select object_name from dba_objects where object_id=20;--游标c1只是得到dba_objects的一个字段列
employee_rec dba_objects%rowtype;--dba_objects; employee_rec得到dba_objects一行字段列
所以
fetch c1 into employee_rec不满足编译条件
------解决思路----------------------
emp%rowtype是记录类的符合变量类型
你可以想象成emp表的一行数据
你改成 dba_objects%rowtype 是 没问题的,值是报了个警告嘛,没有报错啊。
只是因为你可能是hr用户,虽然有dba角色,但对dba_objects的权限上还有些不满足,把查询系统视图的权限给hr