oracle 在 CURSOR FOR LOOP 中使用游标属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18482093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 05:15:21  来源:igfitidea点击:

using cursor attributes in a CURSOR FOR LOOP

oracleloopsfor-loopplsqlcursor

提问by Divas

I am running the following in the Scott schema:

我在 Scott 模式中运行以下内容:

SET serveroutput ON;

BEGIN
FOR c_Emp IN (SELECT * FROM emp)
LOOP
dbms_output.put_line('The record processed by the cursor ' || c_Emp%rowcount);
END LOOP;
end;

This gives the error:

这给出了错误:

cursor attribute may not be applied to non-cursor 'C_EMP'

游标属性可能不适用于非游标“C_EMP”

However if this is done using an explicit cursor it works fine:

但是,如果这是使用显式游标完成的,则它可以正常工作:

set serveroutput on ;

设置服务器输出;

DECLARE 
       emp_record emp%ROWTYPE; 
       count_variable NUMBER;
       CURSOR c IS 
SELECT * FROM emp;
BEGIN
OPEN c;
loop
fetch  c INTO emp_record;
exit WHEN c%notfound;
dbms_output.put_line ('The record count is   ' || c%rowcount);
END loop;
close c;
end;

Just want to understand : whether while using the CURSOR FOR LOOP, is the index variable not a cursor attribute, if so why? could someone plz expalin this....

只是想了解:是否在使用 CURSOR FOR LOOP 时,索引变量不是游标属性,如果是为什么?有人可以解释一下这个....

回答by schurik

c_Empis not the cursor, its a record with felds for each column in the SELECT statment

c_Emp不是游标,它是 SELECT 语句中每一列的字段记录

c_Empis similar to the emp_recordfrom your second example.

c_Emp类似于emp_record你的第二个例子。

回答by Baljeet

Even while using a FOR loop the cursor has to be explicitly defined. A sample use of FOR loop with a cursor would look like below:

即使在使用 FOR 循环时,也必须明确定义游标。带有游标的 FOR 循环的示例使用如下所示:

declare
 cursor c1 is select a from table;
begin
 FOR b in c1
 loop
  <required logic>
 end loop;
end;

回答by gary sze

To get the index in a for loop, you can add the rownumpseudocolumn in the select clause of implicit cursor.

要获取for循环中的索引,可以在隐式游标的select子句中添加rownum伪列。

SET serveroutput ON;

BEGIN
  FOR c_Emp IN (SELECT e.*, rownum FROM emp e)
  LOOP
    dbms_output.put_line('The record processed by the cursor ' || c_Emp.rownum);
  END LOOP;
end;