Skip to content
Advertisement

Add new column with Boolean in PL/SQL

I’m learning PL/SQL right now and I have a doubt. I have created the following table called tbProducts:

And I have inserted some values so the table is like this:

Now what I’m looking for is a boolean variable that can be called bUpdate that returns FALSE if today’s date (26-AUG-2021) is greater than dLastDate and returns TRUE if it’s less or equal so the table would look like this:

I am trying doing the following:

I get an error saying that a FROM was expected after the SELECT statement. Since I’m still learning I don’t know what it’s wrong in this statement, could someone help me? Is there a way to do it with a CURSOR too? Thank you all!

Advertisement

Answer

What you try wouldn’t work. If you want an additional column, you need to add the column to the table with an ALTER TABLE command – but in this case you’re adding a non-deterministic expression and that cannot be added as a virtual column.

The easiest way to achieve what you want is to create a view on top of the table with the case statement as illustrated below:

If you insist on adding a new column then this is what you’d do.

if you just want to display the true/false in the console with a pl/sql block then this is an option (using implicit cursor for loop):

note 1 : pl/sql has a boolean datatype, but sql does NOT have one. So in your table you’ll need to store a string (1/0, T/F, Y/N, TRUE/FALSE).

note 2 : it’s not a good practice to use camelcase in table names or column names. If you’re creating them without quotes the names are case insensitive anyway.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement