日期:2014-05-17  浏览次数:20944 次

oacle 的for循环, 运行时有个错误 求解!跪求
set serveroutput on;
declare
type user_record is record
( v_id emp.empno%type,
  v_name emp.ename%type
);
v_user_record user_record;
 
cursor zzucursor(v_type number)is
select empno,ename from emp 
where deptno =v_type;

begin 
for v_user_record in zzucursor(30) loop
dbms_output.put_line('userid :'||v_user_record.v_id||'username :'||v_user_record.v_name);
end loop ;
end;



错误信息如下:

第 14 行出现错误:
ORA-06550: 第 14 行, 第 49 列:
PLS-00302: 必须声明 'V_ID' 组件
ORA-06550: 第 14 行, 第 1 列:
PL/SQL: Statement ignored




------解决方案--------------------
declare
type user_record is record
( v_id emp.empno%type,
v_name emp.ename%type
);
v_user_record user_record;
cursor zzucursor is
select empno,ename from emp;
begin 
for i in zzucursor loop
 v_user_record.v_id := i.empno;
 v_user_record.v_name := i.ename;
dbms_output.put_line('userid :'||v_user_record.v_id||'username :'||v_user_record.v_name);
end loop ;
end;
/

你没有对v_id赋值,所以产生问题
------解决方案--------------------
对于for 循环而言 for后面的那个变量的作用域只仅限于for这个循环体内, 你之前声明的record的类型在for循环体内并没有被读到,而且是被你再for后面声明的变量给覆盖掉了,不管名字是否一样。
SQL code

declare
type user_record is record
( v_id emp.empno%type,
  v_name emp.ename%type
);
v_user_record user_record;
 
cursor zzucursor is
select empno,ename from emp  
where deptno =30;
begin 
select empno,ename into v_user_record from emp  
where deptno =30 and empno=7698;
dbms_output.put_line('aaaaauserid :'||v_user_record.v_id||'username :'||v_user_record.v_name);
for v_user_record in zzucursor loop
select empno,ename into v_user_record from emp  
where deptno =30;
dbms_output.put_line('userid :'||v_user_record.empno||'username :'||v_user_record.ename);
end loop ;
dbms_output.put_line('bbbbbbbuserid :'||v_user_record.v_id||'username :'||v_user_record.v_name);
end;