Home > Database > Mysql Tutorial > PL/SQL的BOOLEAN的三个值:TRUE,FALSE,NULL

PL/SQL的BOOLEAN的三个值:TRUE,FALSE,NULL

WBOY
Release: 2016-06-07 17:27:05
Original
1216 people have browsed it

Oracle的PL/SQL中的BOOLEAN类型有三个取值,分别是TRUE、FALSE以及NULL。这三个值会给开发的时候造成不必要的麻烦。

Oracle的PL/SQL中的BOOLEAN类型有三个取值,分别是TRUE、FALSE以及NULL。这三个值会给开发的时候造成不必要的麻烦,例如

DECLARE
  B_FLAG_TRUE BOOLEAN := TRUE;
  B_FLAG_FALSE BOOLEAN := FALSE;
  B_FLAG_NULL BOOLEAN :=NULL;
BEGIN
    IF (B_FLAG_NULL AND B_FLAG_TRUE) THEN
        DBMS_OUTPUT.PUT_LINE('NULL AND TRUE');
    END IF;
    IF NOT (B_FLAG_NULL AND B_FLAG_TRUE) THEN
        DBMS_OUTPUT.PUT_LINE('NOT (NULL AND TRUE)');
    END IF;
END;

上面的两个判断,均没有输出字符,,因此,我们需要看一下问题出在什么地方:

DECLARE
  B_FLAG_TRUE BOOLEAN := TRUE;
  B_FLAG_FALSE BOOLEAN := FALSE;
  B_FLAG_NULL BOOLEAN :=NULL;
  B_FLAG_RESULT BOOLEAN;
  FUNCTION TSBOOLEAN (B_FLAG BOOLEAN)RETURN VARCHAR2 IS
    BEGIN
      RETURN CASE B_FLAG
                    WHEN TRUE THEN 'TRUE'
                    WHEN FALSE THEN 'FALSE'
                    ELSE 'NULL'
                  END;
  END TSBOOLEAN;
BEGIN
  B_FLAG_RESULT :=(B_FLAG_NULL AND B_FLAG_TRUE) ;
  DBMS_OUTPUT.PUT_LINE(TSBOOLEAN(B_FLAG_RESULT));
    IF B_FLAG_RESULT THEN
        DBMS_OUTPUT.PUT_LINE(''||'==>'||'');
    END IF;
    IF NOT B_FLAG_RESULT THEN
        DBMS_OUTPUT.PUT_LINE(''||'==>'||'');
    END IF;
END;

原来,上面的输出结果是NULL,所以有一个规范:“我们必须要预料到布尔表达式的结果会存在NULL值,因此必须使用NVL()函数来避免不可预料的结果的发生!”。根据实际的结果值进行如下操作:

DECLARE
  B_FLAG_TRUE BOOLEAN := TRUE;
  B_FLAG_FALSE BOOLEAN := FALSE;
  B_FLAG_NULL BOOLEAN :=NULL;
  B_FLAG_RESULT BOOLEAN;
BEGIN
  B_FLAG_RESULT := (NVL(B_FLAG_NULL,FALSE) AND B_FLAG_TRUE);
    IF (B_FLAG_RESULT) THEN
        DBMS_OUTPUT.PUT_LINE('NULL AND TRUE');
    END IF;
    IF NOT B_FLAG_RESULT THEN
        DBMS_OUTPUT.PUT_LINE('NOT (NULL AND TRUE)');
    END IF;
END;

linux

Related labels:
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template