我有一种情况,我需要先检查一个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:游标已经打开
如何检查光标是否有值并执行循环?
答案 0 :(得分: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
循环中再次使用它来打开光标。
答案 1 :(得分: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;