如何编写一个条件来做一些事情,其中 for 循环 returns 没有行
How to write a condition to do something where a for loop returns no rows
在这个循环中我想捕获循环中不返回数据的函数来执行else命令或者没有数据时的异常。但这不起作用。请用正确的语法帮助我解决问题,谢谢!!
begin
--looping
for i in (
select x, y, z, rownum
from period
where x = 0 -- here this query does not return a row
) loop
begin
if sql%rowcount >=1 -- tried row count
then
dbms_output.put_line ('blablabla');
else
dbms_output.put_line ('blueeeeeee');
end if;
exception when no_data_found --tried this exception
then
dbms_output.put_line ('black');
end;
end loop;
end;
如果您在 for-loop 中的查询没有 return 行,您将永远无法到达此循环的主体,因此
您应该使用 open-cursor-fetch-close 或手动检查,例如:
declare
loop_flag boolean:=false;
loop_n int:=0;
begin
--looping
for i in (
select x, y, z, rownum
from period
where x = 0 -- here this query does not return a row
)
loop
-- processing fetched row:
loop_n:=loop_n+1;
loop_flag:=true;
if loop_n = 1
then
dbms_output.put_line ('first row: ' || r.x );
else
dbms_output.put_line ('other rows...');
end if;
end loop;
if not loop_flag then
dbms_output.put_line ('no data found...');
end;
end;
在这个循环中我想捕获循环中不返回数据的函数来执行else命令或者没有数据时的异常。但这不起作用。请用正确的语法帮助我解决问题,谢谢!!
begin
--looping
for i in (
select x, y, z, rownum
from period
where x = 0 -- here this query does not return a row
) loop
begin
if sql%rowcount >=1 -- tried row count
then
dbms_output.put_line ('blablabla');
else
dbms_output.put_line ('blueeeeeee');
end if;
exception when no_data_found --tried this exception
then
dbms_output.put_line ('black');
end;
end loop;
end;
如果您在 for-loop 中的查询没有 return 行,您将永远无法到达此循环的主体,因此 您应该使用 open-cursor-fetch-close 或手动检查,例如:
declare
loop_flag boolean:=false;
loop_n int:=0;
begin
--looping
for i in (
select x, y, z, rownum
from period
where x = 0 -- here this query does not return a row
)
loop
-- processing fetched row:
loop_n:=loop_n+1;
loop_flag:=true;
if loop_n = 1
then
dbms_output.put_line ('first row: ' || r.x );
else
dbms_output.put_line ('other rows...');
end if;
end loop;
if not loop_flag then
dbms_output.put_line ('no data found...');
end;
end;