日期:2014-05-16  浏览次数:20454 次

oracle中的空串和空格
  在编写存储过程的时候出现了这样的问题:
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 dictionaryId = null or dictionaryId='' then
     dicId:='30298'; --报警时间
  else
     dicId:=dictionaryId;
  end if;
编译时警告:hint:comparison with null in '存储过程名';报警的就是红色字体所在的部分,修改为: if dictionaryId is null or dictionaryId=' '/*空格*/ 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,
即可!