简体   繁体   English

在PL / SQL集合中使用循环索引变量无效

[英]Loop index variable use is invalid in PL/SQL Collections

After I'm executing this I receive the following: 执行此操作后,我收到以下信息:

PL/SQL: ORA-00942: table or view does not exist
PLS-00364: loop index variable 'EMP_REC' use is invalid

It is said that: 据说:

In a block or subprogram, user-defined records are instantiated when you enter the block or Subprogram. 在块或子程序中,当您输入块或子程序时,将实例化用户定义的记录。 They cease to exist when you exit the block Subprogram." 当您退出块子程序时,它们不再存在。”

Now I think I fully understand what it is saying, I think... But along with this said, is it supposed my FOR section to work right?(why it isn't?), because as I can see, the whole things happen in the block not outside. 现在我想我完全理解它的意思,我想...但是与此同时,是否应该说我的FOR部分可以正常工作?(为什么不是?),因为我可以看到,整个过程发生在没有外面的街区。 So till the exit of the block I suppose that v_myrec to exist in the cache or buffer of the private memory allocated by the Oracle server by default because after all v_myrec is a "table" so to speak, therefore the DBMS package should be able to print my "emp_rec.v_sal..." and after the execution of my block completes successfully, THEN v_myrec ceases to exist. 因此,在该块退出之前,我认为v_myrec默认存在于Oracle服务器分配的私有内存的高速缓存或缓冲区中,因为毕竟v_myrec可以说是一个“表”,因此DBMS包应该能够打印我的“ emp_rec.v_sal ...”,然后成功执行我的块,然后v_myrec不再存在。 I'm little confused here, can anybody explain me this? 我在这里有点困惑,有人可以解释一下吗?

If I got it all wrong please correct me. 如果我弄错了,请纠正我。 v v

DECLARE
 TYPE t_rec IS RECORD
  (v_sal           NUMBER(8) NOT NULL := 0,
   v_min_sal       NUMBER(8) DEFAULT 1000,
   v_hire_date     employees.hire_date%TYPE,
   v_rec1          employees%ROWTYPE);
 v_myrec t_rec;  
BEGIN
 v_myrec.v_sal := v_myrec.v_min_sal + 500;
 v_myrec.v_hire_date := SYSDATE;
  SELECT *
  INTO v_myrec.v_rec1
  FROM employees
  WHERE employee_id = 100;
 DBMS_OUTPUT.PUT_LINE(v_myrec.v_rec1.last_name||' '||v_myrec.v_sal||
                      ' '||v_myrec.v_rec1.salary);

 FOR emp_rec IN (SELECT *
                        FROM v_myrec)   
 LOOP
 DBMS_OUTPUT.PUT_LINE(emp_rec.v_sal,...,...);                
 END LOOP;
END;

They are separate errors; 它们是单独的错误; PLS-00364: loop index variable 'EMP_REC' use is invalid is a knock-on error from your cursor being invalid when it's declared, which gets PL/SQL: ORA-00942: table or view does not exist . PLS-00364: loop index variable 'EMP_REC' use is invalid是由于声明时游标无效而导致的连锁错误,导致PL/SQL: ORA-00942: table or view does not exist

v_myrec is not a table. v_myrec不是表。 If it was a SQL (schema-level) collection type rather than a PL/SQL collection you could do: 如果它是SQL(架构级别)集合类型而不是PL / SQL集合,则可以执行以下操作:

FOR emp_rec IN (SELECT * FROM TABLE(v_myrec))

but it isn't, so you can't. 但这不是,所以您不能。 You can't refer to a PL/SQL collection in plain SQL, even inside a PL/SQL block. 即使在PL / SQL块内部,也不能用纯SQL引用PL / SQL集合。 And this isn't even a collection, it's just a single record type, so looping over it doesn't really make much sense anyway. 而且这甚至不是一个集合,它只是一个记录类型,因此循环遍历实际上并没有多大意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM