Informix

Informix

Connect with Db2, Informix, Netezza, open source, and other data experts to gain value from your data, share insights, and solve problems.

 View Only
Expand all | Collapse all

SQL COALESCE() does not support SQL parameters

  • 1.  SQL COALESCE() does not support SQL parameters

    Posted Thu October 12, 2023 02:09 PM

    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
    ------------------------------


  • 2.  RE: SQL COALESCE() does not support SQL parameters

    Posted Thu October 12, 2023 02:22 PM

    Put in an RFE (Request For Enhancement) for that feature then. The site for those not aware is:

    IBM Data and AI Ideas Portal for Customers



    ------------------------------
    Art S. Kagel, President and Principal Consultant
    ASK Database Management Corp.
    www.askdbmgt.com
    ------------------------------



  • 3.  RE: SQL COALESCE() does not support SQL parameters

    Posted Thu October 12, 2023 02:25 PM
    Documented 'feature'  - https://www.ibm.com/docs/en/informix-servers/12.10?topic=expressions-coalesce-function

    Cannot be a host variable or a BYTE or TEXT object



    On 10/12/2023 1:09 PM, Sebastien FLAESCH via IBM TechXchange Community wrote:
    0100018b251405cb-bad0c3da-1717-4f7f-b7e1-4ba0c259f1e0-000000@email.amazonses.com">
    Hello! Seems that the nice COALESCE() SQL function does not support SQL parameters. I get this error:       -999: Not implemented yet. Any...
    IBM TechXchange Community

    Informix

    Post New Message
    SQL COALESCE() does not support SQL parameters
    Reply to Group Reply to Sender
    Sebastien FLAESCH
    Oct 12, 2023 2:09 PM
    Sebastien FLAESCH

    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
    ------------------------------
      Reply to Group Online   View Thread   Recommend   Forward   Flag as Inappropriate  



     
    You are subscribed to "Informix" as paul@oninit.com. To change your subscriptions, go to My Subscriptions. To unsubscribe from this community discussion, go to Unsubscribe.

    --  Paul Watson Oninit www.oninit.com Tel: +1 913 364 0360 Cell: +1 913 387 7529  Oninit® is a registered trademark of Oninit LLC  If you want to improve, be content to be thought foolish and stupid Failure is not as frightening as regret





  • 4.  RE: SQL COALESCE() does not support SQL parameters

    Posted Fri October 13, 2023 01:15 PM

    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
    ------------------------------



  • 5.  RE: SQL COALESCE() does not support SQL parameters

    Posted Fri October 13, 2023 01:31 PM
    Edited by Sebastien FLAESCH Fri October 13, 2023 01:33 PM

    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
    ------------------------------



  • 6.  RE: SQL COALESCE() does not support SQL parameters

    Posted Fri October 13, 2023 02:16 PM

    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
    ------------------------------



  • 7.  RE: SQL COALESCE() does not support SQL parameters

    Posted Fri October 13, 2023 02:42 PM
    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.








  • 8.  RE: SQL COALESCE() does not support SQL parameters

    Posted Fri October 13, 2023 02:37 PM

    Sebastien:

    A couple of points:

    1. Coalesce() and NVL() may appear more elegant, but both are functions which have to iterate while the CASE statement is internal to the SQL parser and will, I believe, execute faster. After some limited testing on an int column in a table with 387000 rows, they run about the same.Testing with a char(128) column on the same table, coalesce() and CASE ran about the same while NVL was about 3X faster! 
    2. I don't understand why testing the value of the variable in code to be used in the CASE test would involve complex processing. Personally, I would just select the column and test for NULL after the fact when the data was returned. Testing for NULL in 4GL is trivial and if NULL you can replace the NULL with your default value in general. In your case I would just select both column1 and column2 and, again, decide in code space whether to report/use column1 or column2 depending on whether the value in the local variable is or is not NULL. You are just overthinking this.
    3. The above does not change the fact that an RFE is called for, but that won't help you. The suggestions in #2 above will.

    Art



    ------------------------------
    Art S. Kagel, President and Principal Consultant
    ASK Database Management Corp.
    www.askdbmgt.com
    ------------------------------



  • 9.  RE: SQL COALESCE() does not support SQL parameters

    Posted Sat October 14, 2023 04:03 AM

    Hello Art,

    About (2) - testing the variable for NULL in the code before using it in the main SELECT: That's the first suggestion I made, but they told me that the rules (WHERE clause conditions) are too complex. I assume the values of the second argument of NVL(var,col) (the SQL column), varies according to the current row. They also want to avoid additional SQL ping-pong with the engine. I have seen a dozen of WHERE conditions requiring this NULL checking. 

    The CASE solution is valid SQL and they are happy with this.

    About (3) - The RFE: I would rather consider this as a strategic decision to have NVL() and COALESCE() accept host variables like other DB engines do. 

    Seb.



    ------------------------------
    Sebastien FLAESCH
    ------------------------------



  • 10.  RE: SQL COALESCE() does not support SQL parameters

    Posted Sat October 14, 2023 04:07 AM

    FYI: https://ibm-data-and-ai.ideas.ibm.com/ideas/INFX-I-568



    ------------------------------
    Sebastien FLAESCH
    ------------------------------