【问题标题】:check for cursor found and loop the cursor in pl sql检查找到的游标并在pl sql中循环游标
【发布时间】:2013-11-05 16:11:26
【问题描述】:

我有一种情况,我需要先检查一个 select 语句是否返回行,然后循环它。在它下面我在做什么高水平。

CURSOR euip_info
IS
SELECT
e.TRANS_ID
from
EQUIPINFO e
where
and e.ORD_NO = s_no;

euip_info_t  euip_info%ROWTYPE;

BEGIN
       OPEN euip_info;
       FETCH euip_info INTO euip_info_t;

        IF euip_info%FOUND THEN

         FOR i in euip_info
           LOOP
           //Do something
           END LOOP;       
        ELSE
          //Do otherthing       
        END IF; 
END

但是当光标具有转到循环部分的值时,我会遇到错误。

ORA-06511: PL/SQL: 游标已打开

如何检查光标是否有值并进行循环?

【问题讨论】:

  • 为什么需要知道它是否有值?只有当光标有值时,循环才会发生。

标签: database oracle plsql


【解决方案1】:

你可以这样做:

CURSOR euip_info
  IS
    SELECT e.TRANS_ID
      FROM EQUIPINFO e
    WHERE e.ORD_NO = s_no;

  euip_info_t  euip_info%ROWTYPE;

BEGIN
  OPEN euip_info;
  FETCH euip_info INTO euip_info_t;

  IF euip_info%FOUND THEN
    LOOP
      EXIT WHEN euip_info%NOTFOUND;
      -- do something with euip_info_t

      -- fetch next record
      FETCH euip_info INTO euip_info_t;
    END LOOP;       
  ELSE
    --Do other thing       
  END IF; 

  CLOSE euip_info;
END;

问题是您试图在 FOR 循环中再次使用它打开光标。

【讨论】:

    【解决方案2】:

    您可以简单地这样做来迭代光标:

    declare
      cursor my_cur is 
      select col1, col2
      from my_table;
    
      l_cnt number := 0;
    
    begin
      for rec in my_cur
      loop
        l_cnt := l_cnt + 1;
        -- do something with rec.col1, rec.col2 
    
      end loop;
      if (l_cnt = 0) then
        -- the cursor was empty
    
      end if;
    end;
    

    【讨论】:

    • 是的。但我还需要检查光标是否为空,以便在它为空时执行其他操作
    猜你喜欢
    • 2012-03-27
    • 2012-04-28
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-12
    相关资源
    最近更新 更多