我经常这样写。 declare a number(2) begin select 1 into a where exists(select * from tablename) exception when no_data_found then //未找到数据 a := 0 when others then return -1 //数据库异常 end;if a=1 then //do something else then //do something others end if;
用游标 可判断%notfound,这个方法好!
如果记录数多的话游标慢 declare i integer; begin select count(1) into i from table_name; if i>0 then dbms_output.put_line(i); else dbms_output.put_line('is null'); end if; end;
这里的 exists做成一个函数 ,在函数里面判断
游标的属性应该是
游标名%found ,用它来判断是否有没有查到数据
若是 游标名%notfound 则结果集为null
declare a number(2)
begin
select 1 into a
where exists(select * from tablename)
exception
when no_data_found then //未找到数据
a := 0
when others then
return -1 //数据库异常
end;if a=1 then
//do something
else then
//do something others
end if;
可判断%notfound,这个方法好!
declare
i integer;
begin
select count(1) into i from table_name;
if i>0 then
dbms_output.put_line(i);
else
dbms_output.put_line('is null');
end if;
end;