oracle function 中的空值判断问题

ainol2009 2010-01-20 04:18:26
比如在某一oracle function中有如下代码:

dd varchar2(20);

select aa into dd from table_A where bb= 'c'; // sql_a

IF dd = ? THEN

-- do something
END IF;

.....


提问:若sql_a找不到记录,然后IF语句是判断是否返回记录的, 请问 上述代码中的 ? 究竟是个什么值,我尝试过“ dd = null ”,“dd is null”,“ dd = '' ”都不行,知道的请回帖一下,谢谢!
...全文
1033 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ainol2009 2010-01-20
  • 打赏
  • 举报
回复
谢谢楼上几位的提醒,原来我是加了异常处理了,我还以为程序在正常运行的 ,所以上述的说明不正确的,我现在用异常处理解决问题了,谢谢大家!~
suiziguo 2010-01-20
  • 打赏
  • 举报
回复
这里应该是异常处理的概念了,而不是判断。
select aa into dd from table_A where bb= 'c';
如果无返回结果,就将触发异常。

SQL> set serveroutput on
SQL>
SQL> declare
2 v_test char(1);
3 begin
4 select * into v_test from dual where 1=2;
5 if v_test is null then
6 dbms_output.put_line('哈哈,为空!');
7 end if;
8 end;
9 /

declare
v_test char(1);
begin
select * into v_test from dual where 1=2;
if v_test is null then
dbms_output.put_line('哈哈,为空!');
end if;
end;

ORA-01403: 未找到数据
ORA-06512: 在 line 4

SQL>
SQL> declare
2 v_test char(1);
3 begin
4 begin
5 select * into v_test from dual where 1=2;
6 exception
7 when no_data_found then
8 dbms_output.put_line('爱慕所瑞,为空!');
9 end;
10 end;
11 /

爱慕所瑞,为空!

PL/SQL procedure successfully completed

SQL>
wuyisky84 2010-01-20
  • 打赏
  • 举报
回复
exception
when no_data_found then
do something;
end ;



没有记录的时候用 no_data_found 捕获
Hemes_MC 2010-01-20
  • 打赏
  • 举报
回复
select aa into dd from table_A where bb= 'c';

这句话找不到数据会进异常,你可以这样去写:
begin
select aa into dd from table_A where bb= 'c';
exception
when no_data_found then
dd := null;
end

if dd is null then
....

调调代码,可能有些地方要改

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧