declare
no emp.deptno%type;
cursor c1 is select * from emp where deptno=&no;
begin
open c1;
if (c1%rowcount)=0 then
dbms_output.put_line('没有此部门号!');
end if;
close c1;
for i in c1 loop
dbms_output.put_line(i.ename||''||i.job||''||i.sal);
end loop;
end;
/
no emp.deptno%type;
cursor c1 is select * from emp where deptno=&no;
begin
open c1;
if (c1%rowcount)=0 then
dbms_output.put_line('没有此部门号!');
end if;
close c1;
for i in c1 loop
dbms_output.put_line(i.ename||''||i.job||''||i.sal);
end loop;
end;
/
dbms_output.put_line('没有此部门号!');
------------------------------------------
这个是判断没有数据发现时的情况,但你的是游标的for循环,而for循环的一个作用是,当没有数据发现是,游标自动关闭,所以,上面的判断是没用的,因为在for循环中不可能出现找不到数据的情况。
declare
ename emp.ename%type;
job emp.job%type;
sal emp.sal%type;
cursor c1(no emp.deptno%type)
is select * from emp where deptno=no;
begin
open c1(@num);
fetch c1 into ename,job,sal;
if (c1%notfound) then
dbms_output.put_line('没有此部门号!');
end if;
while c1%found
loop
dbms_output.put_line(ename||''||job||''||sal);
fetch c1 into ename,job,sal;
end loop;
end;
--用显示游标来看看,你看看上面的效果
RAISE_APPLICATION_ERROR(-20007,'没有此部门号!');
end if;也可以.
declare
eshow emp%rowtype;
cursor c1(no emp.deptno%type)
is select * from emp where deptno=no;
begin
open c1(&num);
fetch c1 into eshow;
if (c1%notfound) then
dbms_output.put_line('没有此部门号!');
end if;
while c1%found
loop
fetch c1 into eshow;dbms_output.put_line(eshow.ename||''||eshow.job||''||eshow.sal);
end loop;
close c1;
end;
/
while c1%found
表示有记录的时候。
while c1%found
表示有记录的时候。
__________________________________________
什么呀!不明白你说的什么.
-------------------------------------------
上面执行只是一次,是为以后的c1%found作准备,
while-loop-end中的是一个循环的。
没有测试
cursor c1 (no emp.deptno%type) is
select *
from emp
where deptno = no; v_cnt integer := 0;
begin
for i in c1 (&num) loop
dbms_output.put_line (i.ename || '' || i.job || '' || i.sal);
v_cnt := v_cnt + 1;
end loop; if v_cnt = 0 then
dbms_output.put_line ('ûÓд˲¿ÃźÅ!');
end if;
end;
/
cursor c1 (no emp.deptno%type) is
select *
from emp
where deptno = no; v_cnt integer := 0;
begin
for i in c1 (&num) loop
dbms_output.put_line (i.ename || '' || i.job || '' || i.sal);
v_cnt := v_cnt + 1;
end loop; if v_cnt = 0 then
dbms_output.put_line ('没有此部门号!');
end if;
end;
____________________________________________________________________________________--
只要CURSOR是OPEN的,不管OPEN CURSOR的结果集是否是空,在FETCH时ORACLE都不会报错,结束控制是需要用%FOUND来判断的;而%FOUND的判断只能出现在FETCH之后。
CURSOR在使用FOR循环时,系统隐式OPEN CURSOR,再FETCH到循环变量,然后判断%FOUND,FALSE就结束循环且自动CLOSE CURSOR。这一切都是隐式的;而其他方式的循环是要显式OPEN、FETCH、%FOUND,都需要隐式的。所以WHILE时就需要两次FETCH。