PLSQL中NULL值的影响

什么是NULL?

在Oracle中,一个缺失值由NULL来表示。 空字符串:‘’;

‘’ is null 该表达式为true;

declare 
  -- Local variables here
 
begin
  -- Test statements here
  if '' is null then 
      dbms_output.put_line(1);
      end if;
end;

运行结果:

在PL/SQL中,给一个varcahr2(n)的变量赋值长度为0的字符串,也能得到NULL。

declare 
  -- Local variables here
 str varchar2(1) := '';
begin
  -- Test statements here
  if str is null then 
      dbms_output.put_line(2);
      end if;
end;

运行结果:

对这个结果是不是和吃惊!

Oracle数据库会把空字符串当做NULL处理。

declare
  empty_varchar2 varchar2(10) := '';
  empty_char     char(10) := '';
begin
  -- Test statements here
  if empty_varchar2 is null then
    dbms_output.put_line('empty_varchar2 is NULL');
  end if;

  if '' is null then
    dbms_output.put_line(''''' is NULL');
  end if;

  if empty_char is null then
    dbms_output.put_line('empty_char is NULL');
  elsif empty_char is not null then
    dbms_output.put_line('empty_char is not NULL');
  end if;

end;

运行结果:

char类型的变量不会被看作是 NULL, 因为char类型是一个固定长度的字符串,不会为空,会用空格去填补一直到达到10个字符串为止。 

varchar2类型的变量是NULL,是一个长度为0的文本字符串,在if语句中的判断要注意,因为NULL永远不等于NULL

if user_entered_name <> user_from_database then 

如果用户输入是空字符串,这个if永远得不到true。 因为NULL永远不等于NULL

解决空字符串就是NULL的方法:

if (user_entered_name <> user_from_database) or (user_entered_name is null) then 

NULL对条件判断的影响

布尔表达式返回的结果是三种:TRUE、FALSE、NULL

示例:工资超过4000的员工获得奖金,而其它员工将没有奖金

错误代码:

declare
  -- Local variables here
  salary number := 4001;
begin
  -- Test statements here
  if salary <= 4000 then
    dbms_output.put_line('没有奖金');
  else
    dbms_output.put_line('奖金500');
  end if;
end;

运行结果:

但,如果当员工的工资为NULL的时候,会执行奖金为500的情况。

declare
  -- Local variables here
  salary number := null;
begin
  -- Test statements here
  if salary <= 4000 then
    dbms_output.put_line('没有奖金');
  else
    dbms_output.put_line('奖金500');
  end if;
end;

运行结果:

所以条件判断中要考虑NULL值的存在。

NULL语句

null;

短路估计

当第一个操作数是FALSE或NULL的时候,立即执行的其它分支

if condition1 and condition2 then 
  ...
  else
    ...
    end if;

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PLSQL文字符乱码可能是由于服务器端和客户端的编码不一致导致的。有几种解决办法可以尝试: 一种解决办法是检查服务器端的编码设置。可以通过执行以下SQL语句来查看数据库的字符集: ``` select userenv('language') from dual ``` 然后执行以下SQL语句来查看本地字符集: ``` select * from V$NLS_PARAMETERS ``` 确认第一行PARAMETER项的NLS_LANGUAGE对应的VALUE项是否和第一步得到的一样。如果不一样,则需要设置环境变量。 另一种解决办法是设置本地环境变量NLS_LANG。可以按照以下步骤进行设置: 1. 打开计算机属性的高级系统设置。 2. 进入环境变量,新建一个变量名为NLS_LANG,变量为第一步查询到的。例如,如果查询到的为AMERICAN_AMERICA.ZHS16GBK,那么变量就设为AMERICAN_AMERICA.ZHS16GBK。 重新打开PLSQL,执行SQL语句,应该就能解决文字符乱码的问题了。 请注意,以上方法适用于在服务器上使用的Oracle数据库,而不是本机的Oracle数据库。确保根据你的实际情况进行设置。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [plsql文乱码](https://blog.csdn.net/weixin_39417044/article/details/128610027)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [PLSQL查询结果文显示乱码](https://blog.csdn.net/p419228530/article/details/125967880)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值