【场景】 在存储过程中,我要判断某个表的记录数是否为0的实现方法【做法一】select count(*) into num from test;if num=0 then ...... else ...... end if; 除了上面的做法外,能否类似于用下面的做法来实现 ? if (select count(*) from test)=0 then ...... end if;
--捕获异常的方式做: declare num number; begin select count(*) into num from test; --记录数不为0的操作 ..... ..... exception when no_data_found then --记录数为0的操作 ...... ...... end;
我试了下 好像if (select count(*) from test)=0 then不行啊
if (select count(*) from test)=0……这种不行
if条件要求是表达式,sql语句不成.
SQL语句中可以用case when 啊
select decode(t.cn,0,0,t.cn) from (select count(*) cn from test) t
--捕获异常的方式做:
declare
num number;
begin
select count(*) into num from test;
--记录数不为0的操作
.....
.....
exception when no_data_found then
--记录数为0的操作
......
......
end;
select decode(t.cn,0,0,t.cn) from (select count(*) cn from test) t