0

我有一种情况,我需要先检查一个 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: 游标已打开

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

4

2 回答 2

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问题是您试图在循环中再次打开游标。

于 2013-11-05T16:20:46.330 回答
0

您可以简单地执行此操作来迭代光标:

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;
于 2013-11-05T16:24:51.460 回答