0

我正在尝试使用游标打印字段的前 100 个值。不过,这里出现 ORA-06550 错误。有人能告诉我我缺少什么吗?

Declare 
  BG_TOTAL number;

  cursor c1 is 
    select BG_ID 
      from <tablename>;

  Type BG_TAB_TYPE is table of  c1%ROWTYPE;

  BG_LIST BG_TAB_TYPE;

Begin
  open c1;
  FETCH c1 BULK COLLECT INTO BG_LIST;

  close c1;

   for i in 1..c1.count    
   loop
     DBMS_OUTPUT.PUT_LINE(c1(i).BG_ID);
   End loop;  
end;
4

2 回答 2

1

另一种选择是bulk collectlimit.

这很好地将内容、限制和处理分开。在您的情况下,分离可能不是问题,但我发现这时不时有用(帮助我编写更易于测试的模块化代码)。

declare
  -- data content
  cursor tables_c is select * from all_tables;
  type table_list_t is table of tables_c%rowtype;

  v_table_list table_list_t;
begin
  open tables_c;

  -- limiting the data set
  fetch tables_c bulk collect into v_table_list limit 8;

  -- processing
  for i in 1 .. v_table_list.count loop
    dbms_output.put_line(v_table_list(i).table_name);
  end loop;

  close tables_c;
end;
/
于 2013-11-07T07:25:08.690 回答
0

您应该循环遍历您批量收集记录的嵌套表,而不是通过游标。这是更正后的代码:

Declare 
  BG_TOTAL number;

  cursor c1 is 
    select BG_ID 
      from my_Tab524;

  Type BG_TAB_TYPE is table of  c1%ROWTYPE;

  BG_LIST BG_TAB_TYPE;

Begin
  open c1;
  FETCH c1 BULK COLLECT INTO BG_LIST;

  close c1;

   for i in 1..BG_LIST.count    
   loop
     DBMS_OUTPUT.PUT_LINE(BG_LIST(i).BG_ID);
     EXIT WHEN i = 10;
   End loop;  
end;
于 2013-11-07T02:17:39.037 回答