Skip to main content

PL/SQL 101: Why can't I display a Boolean value with DBMS_OUTPUT.PUT_LINE?

DBMS_OUTPUT. PUT_LINE is the built-in procedure that PL/SQL developers use to display output on the screen.

Let's watch it do it's thing on LiveSQL:


So I displayed a string, a date, a date converted to a string, and a number. Cool. 

Now let's display a Boolean value (TRUE, FALSE or NULL):


Ouch! It did not like a Boolean value, that's for sure. But why not? To figure that out, we need to take a look at the specification of the DBMS_OUTPUT package.

That's easy in SQL Developer: just right-click and choose Popup Describe. 



Searching for "procedure put_line", I see:

create or replace package dbms_output authid definer as
...
  procedure put_line(a varchar2);

Huh. That's weird. It only accepts a string. So how could it display a number and a date with no problem, but then choke on a Boolean?

It all has to do with implicit conversions. Generally, Oracle Database in both SQL and PL/SQL will automatically and implicitly convert a value from one data type to another "when such a conversion makes sense." This matrix shows the implicit conversions that are supported, but I can summarize for you in terms of this post:

1. Dates, timestamps and numbers can be implicitly converted to strings.
2. Booleans cannot be converted to strings. 

In fact, the BOOLEAN datatype doesn't even appear in the matrix. That is probably in part because BOOLEAN is a PL/SQL-specific datatype; it's not supported at all in Oracle SQL.

Does that mean that it will never be possible for DBMS_OUTPUT.PUT_LINE to display a Boolean? Not at all. For example, the PL/SQL development team could add an overloading for PUT_LINE in the DBMS_OUTPUT package:

create or replace package dbms_output authid definer as
...
  procedure put_line(a varchar2);
  procedure put_line(a boolean);

And then they just have to figure out what to display. That's the "funny" thing about the Boolean values of TRUE and FALSE. They are in English. If you are using a different language, should the text displayed for a Boolean value change to the words for TRUE and FALSE in that language?

So many questions, so little time - but no matter how many questions there are, adding an overloading to this built-in package is well out of our control.

So what's a developer to do?

Well, first of all, please do not do this, over and over again throughout your code:


BEGIN
   IF my_boolean
   THEN
      DBMS_OUTPUT.PUT_LINE ('TRUE');
   ELSIF NOT my_boolean
   THEN
      DBMS_OUTPUT.PUT_LINE ('FALSE');
   ELSE
      DBMS_OUTPUT.PUT_LINE ('NULL');
   END IF;
END;

Instead, why not install a "Boolean Manager" in your environment that allows you to easily display Boolean values (and convert them from/to strings)?



A final note: we recommend that you avoid implicit conversions whenever possible, and tell Oracle Database exactly what you want, and how you want it done. A simple example demonstrating the benefit of being explicit has to do with displaying date values.

When I ask to display a date, as in:

BEGIN
   DBMS_OUTPUT.put_line (DATE '2016-10-31');
END;
/

31-OCT-16

Oracle Database uses the default NLS date format setting for my session to do the implicit conversion. The default format is DD-MON-YY, which honestly very few humans find helpful. But notice in particular that the time component is ignored.

Now let's try it again after changing the session date format:


ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'
/

BEGIN
   DBMS_OUTPUT.put_line (DATE '2016-10-31');
END;
/

2016-10-31 00:00:00

So rather than assuming the session date format is what you hope or desire, make it explicit when you  ask to display a date, as in:

BEGIN
   DBMS_OUTPUT.put_line (TO_CHAR (DATE '2016-10-31'
      , 'YYYY-MM-DD HH24:MI:SS'));
END;
/

31-OCT-16

Comments

  1. Hmmm... Last example a bit too much cut'n'paste? ;-)

    Seems to be missing a final parentheses and the correct output?

    But good points in this post ;-)

    ReplyDelete
  2. I'd prefer this overload enhancement:

    create or replace package standard authid definer as
    ...
    procedure to_char(left boolean);

    ReplyDelete
    Replies
    1. Excellent point, Kevan.

      Don't just fix the problem with PUT_LINE. Fix it re: implicit conversion generally!

      When and if you can start passing a Boolean to PUT_LINE, I expect that is how the PL/SQL dev team will resolve it. 'Cause they are lots smarter than me, and maybe just maybe as smart as you. :-)

      Delete
  3. Hello Sir, why did you want the developers to not to use the IF..THEN..DBMS on the Boolean, but to use the Boolean manager? The Boolean manager must have an IF..THEN.. processing inside of it isn't it?

    Thank you,
    Boobal Ganesan

    ReplyDelete
    Replies
    1. That's precisely my point, Boobal: the utility procedure has the IF-THEN inside it so that YOU don't have to write that over and over again in your application code.

      Delete

Post a Comment

Popular posts from this blog

Quick Guide to User-Defined Types in Oracle PL/SQL

A Twitter follower recently asked for more information on user-defined types in the PL/SQL language, and I figured the best way to answer is to offer up this blog post. PL/SQL is a strongly-typed language . Before you can work with a variable or constant, it must be declared with a type (yes, PL/SQL also supports lots of implicit conversions from one type to another, but still, everything must be declared with a type). PL/SQL offers a wide array of pre-defined data types , both in the language natively (such as VARCHAR2, PLS_INTEGER, BOOLEAN, etc.) and in a variety of supplied packages (e.g., the NUMBER_TABLE collection type in the DBMS_SQL package). Data types in PL/SQL can be scalars, such as strings and numbers, or composite (consisting of one or more scalars), such as record types, collection types and object types. You can't really declare your own "user-defined" scalars, though you can define subtypes  from those scalars, which can be very helpful from the p

The differences between deterministic and result cache features

 EVERY once in a while, a developer gets in touch with a question like this: I am confused about the exact difference between deterministic and result_cache. Do they have different application use cases? I have used deterministic feature in many functions which retrieve data from some lookup tables. Is it essential to replace these 'deterministic' key words with 'result_cache'?  So I thought I'd write a post about the differences between these two features. But first, let's make sure we all understand what it means for a function to be  deterministic. From Wikipedia : In computer science, a deterministic algorithm is an algorithm which, given a particular input, will always produce the same output, with the underlying machine always passing through the same sequence of states.  Another way of putting this is that a deterministic subprogram (procedure or function) has no side-effects. If you pass a certain set of arguments for the parameters, you will always get

My two favorite APEX 5 features: Regional Display Selector and Cards

We (the over-sized development team for the PL/SQL Challenge - myself and my son, Eli) have been busy creating a new website on top of the PLCH platform (tables and packages): The Oracle Dev Gym! In a few short months (and just a part time involvement by yours truly), we have leveraged Oracle Application Express 5 to create what I think is an elegant, easy-to-use site that our users will absolutely love.  We plan to initially make the Dev Gym available only for current users of PL/SQL Challenge, so we can get feedback from our loyal user base. We will make the necessary adjustments and then offer it for general availability later this year. Anyway, more on that as the date approaches (the date being June 27, the APEX Open Mic Night at Kscope16 , where I will present it to a packed room of APEX experts). What I want to talk about today are two features of APEX that are making me so happy these days: Regional Display Selector and Cards. Regional Display Sel