存储过程空值处理
答案:3 悬赏:70
解决时间 2021-01-22 16:43
- 提问者网友:夢醒日落
- 2021-01-22 08:28
存储过程空值处理
最佳答案
- 二级知识专家网友:孤独入客枕
- 2021-01-22 08:37
declare
v_num int;
v_num1 int;
select count(*) into v_num1 from table1;
if v_num1=0
then v_num:=0
else
select a.num1 into v_num from table1 ;
end if;大概就这个意思吧
追问:当select count(*) from table1; 查询不到任何数据的时候,其实count(*)不是0,是空的。
所以into v_num1的时候就会报错了
追答:你把一个表置空,你查一下看是0不,反正我这是0
追问:刚才我也试了下~确实可以,我之前使用过,但是当时有问题,具体情况我记不清了,所以印象中就感觉这么写不行,也可能是我当时判断的有问题。谢谢了。
追答:额,你看看这样弄到你程序里行不行,不行的话再把问题说一下
v_num int;
v_num1 int;
select count(*) into v_num1 from table1;
if v_num1=0
then v_num:=0
else
select a.num1 into v_num from table1 ;
end if;大概就这个意思吧
追问:当select count(*) from table1; 查询不到任何数据的时候,其实count(*)不是0,是空的。
所以into v_num1的时候就会报错了
追答:你把一个表置空,你查一下看是0不,反正我这是0
追问:刚才我也试了下~确实可以,我之前使用过,但是当时有问题,具体情况我记不清了,所以印象中就感觉这么写不行,也可能是我当时判断的有问题。谢谢了。
追答:额,你看看这样弄到你程序里行不行,不行的话再把问题说一下
全部回答
- 1楼网友:老鼠爱大米
- 2021-01-22 10:37
在查询时加判断
select a.num1 into v_num from table1 where a.num1 is not null
或者 单空值时定义固定值
select isnull(a.num1,0) into v_num from table1 where a.num1 is not null
追问:where a.num1 is not null,在这里是没有作用的,因为不是查询到的记录的num1字段是空,而是查询结果根本没有任何记录,什么都没有查询到,在into的时候会报错。
select a.num1 into v_num from table1 where a.num1 is not null
或者 单空值时定义固定值
select isnull(a.num1,0) into v_num from table1 where a.num1 is not null
追问:where a.num1 is not null,在这里是没有作用的,因为不是查询到的记录的num1字段是空,而是查询结果根本没有任何记录,什么都没有查询到,在into的时候会报错。
- 2楼网友:不想翻身的咸鱼
- 2021-01-22 09:20
把这个函数加入数据库,调用函数提前判断一下数据库中有没有数据。
有数据返回1无数据返回0
function fun_ExistRecord (stablename in varchar2,sWhereItem in varchar2) return number
is
ssql varchar2(1000);
iR number(1);
begin
if swhereitem is null then
ssql:='select case when exists ( select * from ' || stablename || ' ) then 1 else 0 end as aa from dual';
else
ssql:='select case when exists ( select * from ' || stablename || ' where ' ||
swhereitem ||' ) then 1 else 0 end as aa from dual' ;
end if ;
execute immediate ssql into ir;
return ir;
end fun_ExistRecord;
有数据返回1无数据返回0
function fun_ExistRecord (stablename in varchar2,sWhereItem in varchar2) return number
is
ssql varchar2(1000);
iR number(1);
begin
if swhereitem is null then
ssql:='select case when exists ( select * from ' || stablename || ' ) then 1 else 0 end as aa from dual';
else
ssql:='select case when exists ( select * from ' || stablename || ' where ' ||
swhereitem ||' ) then 1 else 0 end as aa from dual' ;
end if ;
execute immediate ssql into ir;
return ir;
end fun_ExistRecord;
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯