Three BOOLEAN values of PL/SQL: TRUE, FALSE, NULL

Source: Internet
Author: User
The BOOLEAN type in Oracle PLSQL has three values: TRUE, FALSE, and NULL. These three values cause unnecessary trouble during development.

The BOOLEAN type in Oracle PL/SQL has three values: TRUE, FALSE, and NULL. These three values cause unnecessary trouble during development.

The BOOLEAN type in Oracle PL/SQL has three values: TRUE, FALSE, and NULL. These three values cause unnecessary troubles during development, for example

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;

The above two judgments have no output characters. Therefore, let's take a look at the problem:

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;

Originally, the output result above is NULL, so there is a standard: "We must expect that the result of a Boolean expression will have a NULL value, so we must use NVL () function to avoid unexpected results! ". Perform the following operations based on the actual result value:

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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.