oracle中的空串和空格

[size=large]在编写存储过程的时候出现了这样的问题:
1、循环游标取出字段的值做以下操作时:
写成:
if nvl(c1.text,'')<>'' then
语句1; --当c1.text有值时,if中的语句1不执行
end if;
而写成:
if nvl(c1.text,' ')<>' ' then
语句1; --当c1.text有值时,if中的语句1执行
end if;
2、今天在存储过程中对某个变量做非空判断如下:
--dictionaryId 传入参数
dicId varchar2(20);
begin
if [color=red][b]dictionaryId = null or dictionaryId='' [/b][/color]then
dicId:='30298'; --报警时间
else
dicId:=dictionaryId;
end if;
编译时警告:hint:comparison with null in '存储过程名';报警的就是红色字体所在的部分,修改为: if [color=blue][b]dictionaryId is null or dictionaryId=' '/*空格*/[/b][/color] then 就不报警了,所以做非空判断时,正确的写法是:dictionaryId is null 而不能写成: dictionaryId='',即使null和''在oracle中似乎是等同的;空串判断就是dictionaryId=' '/*空格*/;

3、过程中的临时变量,初始化时
strFileName :='';
在调试存储过程时,该变量在未变化前,查看它的值时总是null.

问:
1、临时变量初始化赋值时该赋成'' 还是 ' '?
2、在循环中,一变量需重复使用,在循环结束前需置空,又该写成什么样啊?


5、今天碰到一個問題,一個oracle存儲過程中的一個參數之前一直傳的是數字,現在有一些特殊的情況需要傳字符,這樣就會報
ORA-06502: PL/SQL: numeric or value error: character to number conversion error的錯誤

if nvl(c1.template_detail_id,'')<>'' then

if if nvl(c1.template_detail_id,' ')<>' ' then--报以上错误

总结:c1.template_detail_id为数字的时候不能写成下一种格式!!!
正确的写法是:if nvl(c1.template_detail_id,0)<>0 then

6、v_summary varchar2(1000); --定义变量
begin
select summary into v_summary from tab_sum;
end;
当summary为null时,该句总是报错,无法往下执行,
尝试初始化变量(v_summary :=' ';),在语句中加nvl[nvl(summary,' ')]还是出错。
解决:select nvl(max(summary),'') from tab_sum;

7、网上遇到一个典型的问题:我都已经用nvl函数了,为何还是有ora-01400错误呢?

insert into tab_test1 nologging
select a.subscription_id,a.account_id,a.customer_id,a.subs_status,a.acct_type,a.service_num,a.service_type,a.service_status,a.rate_class,
a.status_chg_date,a.active_date,a.create_date,a.region_id,
(select nvl(rele_office_id,'0') from tab_test3 where account_id=a.account_id) rele_office_id,
a.operator_id,a.reseller_id,
(select nvl(pay_type,0) from tab_test3 where account_id=a.account_id) pay_type,a.last_oper_type,
(select nvl(county_id,0) from tab_test3 where account_id=a.account_id) county_id,
a.inactive_date
from tab_test2 a

ORA-01400: cannot insert NULL into("OSS"."tab_test1"."RELE_OFFICE_ID")

请问这是什么原因?

楼主挺厚道,还把结果给公布了:
找到解决办法了,谢谢!
(select nvl(rele_office_id,'0') from tab_test3 where account_id=a.account_id) rele_office_id,
改为
nvl((select rele_office_id from tab_test3 where account_id=a.account_id),'0') rele_office_id,
即可!

8、过程中select into中的问题
select 字段 into 变量 from 表 where 条件;
当根据条件查询出的记录数为0时,into变量会报这个错ORA-01403: no data found,所以要做此类操作时,先查询记录数是否大于0再执行select into 变量的语句。

[/size]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值