Home > Database > Mysql Tutorial > Oracle 中使用 select a into b 时遇到空值问题

Oracle 中使用 select a into b 时遇到空值问题

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 17:23:30
Original
1166 people have browsed it

当在PL_SQL中执行SELECT hellip; INTO hellip;语句时,如果返回结果集为空,则回触发NO_DATA_FOUND错误。但是当 SELECT 中有字

今天一朋友问及我这个问题,当记录不存在,,会提示 no data 的错误,下面是网上这类问题的解决方法。

当在PL_SQL中执行SELECT … INTO …语句时,如果返回结果集为空,则回触发NO_DATA_FOUND错误。但是当 SELECT 中有字段用到函数时,即使结果集为空,也不会触发NO_DATA_FOUND错误。

CREATE OR REPLACE PROCEDURE RD.EXCEPTION_TEST IS

test    date;

BEGIN
   
    SELECT END_TIME
          INTO test
          FROM RD.R_LINE_STOP_TIME
        WHERE rownum = 1;
END EXCEPTION_TEST;

当执行该存储过程时,会提示错误
ORA-01403: no data found
ORA-06512: at "RD.EXCEPTION_TEST", line 17
ORA-06512: at line 1

CREATE OR REPLACE PROCEDURE RD.EXCEPTION_TEST IS

test    date;

BEGIN
   
    SELECT max(END_TIME)
          INTO test
          FROM RD.R_LINE_STOP_TIME
        WHERE rownum = 1;
 
END EXCEPTION_TEST;
当执行带有函数的存储过程时,则并不会报no data found异常.
CREATE OR REPLACE PROCEDURE RD.EXCEPTION_TEST IS

test    varchar2(10);
BEGIN
   
    SELECT to_char(END_TIME, 'yyyy-mm-dd')
          INTO test
          FROM RD.R_LINE_STOP_TIME
        WHERE rownum = 1;
END EXCEPTION_TEST;
但是在执行带to_char、substr等这样的函数会报no data found异常。

==========

我的理解是,当表中存在记录但字段值为空值,与表中不存在记录为空是两个不同的概念。一些集合函数如max,sum在处理的时候会自动引入null值。

不知sql server中是否也存在类似的问题?

linux

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
AngularJS Select default value?
From 1970-01-01 08:00:00
0
0
0
How to connect PHP toolbox to oracle database
From 1970-01-01 08:00:00
0
0
0
Select option using PHP loop
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template