Apropos to Jacob's suggestion, you could:
WHERE column1 = COALESCE ( (select ? ), column2 ) ...
That MIGHT work taking advantage of the engine's ability to SELECT a constant with no FROM clause.
Art
Art S. Kagel, President and Principal Consultant
ASK Database Management
Disclaimer: Please keep in mind that my own opinions are my own opinions and do not reflect on the IIUG, nor any other organization with which I am associated either explicitly, implicitly, or by inference. Neither do those opinions reflect those of other individuals affiliated with any entity with which I am affiliated nor those of the entities themselves.
Original Message:
Sent: 10/13/2023 2:16:00 PM
From: Jacob Salomon
Subject: RE: SQL COALESCE() does not support SQL parameters
Sebastien replied to me:
> When using "(?)" you don't use a host variable, it's just a plain SQL string literal.
Correct. I meant the string literal "(?)" but that was a poor choice of literal due to the special meaning of ? as a placeholder for PREPAREing sql statements.
In any case, I found out many years ago that ? could be used only where a literal value would go, not where a column name would go. So I suppose one could try using COALESCE(some-column, ?)
in the query, like this:
select name, coalesce(middle, ?) middle, last from customer;
Of course, this would barf in straight SQL ie. dbaccess. But where it can be PREPAREd ie. ESQL it should also fail; it is being used in a column specification. My Linux system is not booted right now; can someone please check if the syntax I just suggested works in the context of a PREPARE? As I said, it should not work but I might be pleasantly surprised.
------------------------------
Jacob Salomon
---
Nobody goes there anymore, it's too crowded. --Attr: Yogi Berra
------------------------------
Original Message:
Sent: Fri October 13, 2023 01:31 PM
From: Sebastien FLAESCH
Subject: SQL COALESCE() does not support SQL parameters
Hi Jacob,
When using "(?)" you don't use a host variable, it's just a plain SQL string literal.
The request came from user code written in Genero BDL (4GL), where a complex SELECT statement was actually using the NVL() function, which has same limitation regarding host variables.
I thought COALESCE() is more standard and would support host variables like with other DB engines, but it does not with Informix.
The goal is to do use some default value from an SQL column, if the provided host variable is NULL:
.... WHERE column1 = NVL(variable1,column2)
If variable1 is not null, compare column1 to variable1, otherwise, compare to column2.
column1 most never be null of course.
We could solve this by using a CASE instruction (CASE WHEN variable1 IS NOT NULL THEN variable1 ELSE column2 END), but I thought NVL() or COALESCE() is more elegant.
Making sure that the variable1 is not null before executing the main SQL statement would require some (complex) additional SELECT statements before the main SQL statement.
Any best practice for such case is welcome.
Seb
------------------------------
Sebastien FLAESCH
Original Message:
Sent: Fri October 13, 2023 01:15 PM
From: Jacob Salomon
Subject: SQL COALESCE() does not support SQL parameters
Hi Sebastien.
Sorry, I have no better solution than that supplied by Art. Though the "Not implemented yet" error message indicates that IBM does have plas to implement it. Hence, the RFE might be to step on the gas on this one.
But how are you trying to use coalesce()? I have yet to try it but I would think you could still use it with a literal string, like:
select name, coalesce(middle, "(?)") middle, last from customer;
Please post the way you are using it so we can all start clamoring for it like it's a game-changer. <Grin>
------------------------------
Jacob Salomon
---
Nobody goes there anymore, it's too crowded. --Attr: Yogi Berra
Original Message:
Sent: Thu October 12, 2023 02:09 PM
From: Sebastien FLAESCH
Subject: SQL COALESCE() does not support SQL parameters
Hello!
Seems that the nice COALESCE() SQL function does not support SQL parameters.
I get this error: -999: Not implemented yet.
Any bug / feature id?
This would be very useful, it's supported by other DB engines.
Seb
------------------------------
Sebastien FLAESCH
------------------------------