【问题标题】:Toad: how to loop a resultset of selectToad:如何循环选择的结果集
【发布时间】:2016-07-14 07:22:06
【问题描述】:

我正在使用 Toad,有一个名为 MyTable 的表,它有一个名为 INFO 的列:
INFO
abcd
呵呵
伊克尔

我需要的是把INFO的元素一个一个拿来做任务。所以我想我需要像下面这样的东西:

foreach (select INFO from MyTable)
    print
end

我尝试了谷歌,似乎我应该使用 CURSOR。所以我尝试这样:

DEF msg varchar2(15);

cursor cr is
    select info from mytable;

begin
  OPEN cr;
  loop
    FETCH cr into msg;
    exit when cr%NOTFOUND;
    -- do job
  end loop;
  CLOSE cr;
end;

但我得到了一个错误:

光标cr是
第 3 行出错
ORA-00900: 无效的 SQL 语句
脚本在第 3 行终止。

【问题讨论】:

    标签: sql select foreach sqlplus toad


    【解决方案1】:

    显然您想执行PL/SQL 块,但DEF 不是PL/SQL 的一部分。 尝试执行以下代码块:

    declare
    msg varchar2(15);
    cursor cr is
        select info from mytable;
    begin
      OPEN cr;
      loop
        FETCH cr into msg;
        exit when cr%NOTFOUND;
        -- do job
      end loop;
      CLOSE cr;
    end;
    

    你也可以使用cursor for loop statement来做同样的事情

    begin
      for rec in (
        select info from mytable
      ) loop
          -- do job (you can reference info by using rec.info )
      end loop;
    end;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-10
      • 1970-01-01
      • 2021-05-05
      • 2015-01-28
      • 1970-01-01
      • 2019-02-18
      • 1970-01-01
      相关资源
      最近更新 更多