Search code examples
oracle-databasefunctionora-00907

Oracle 9i: Supposedly missing right parenthesis


I have a simple function written in Oracle 9i (version 9.2.0.4.0) to simulate an in-line IF. For those interested, here's the code:

create or replace
FUNCTION IIF  
   (testExpression NUMBER,
    trueResult NUMBER,
    falseResult NUMBER)
RETURN NUMBER   
AS
BEGIN
   /*
      A simple in-line IF function for use with SQL queries. If the test
      expression evaluates to any non-zero value, it is considered to be 
      true, and the trueResult is returned. Otherwise, falseResult is
      returned.
   */   
   IF (testExpression is null) or (testExpression = 0) THEN
      return falseResult;
   ELSE
      return trueResult;
   END IF;
END IIF;

This isn't rocket science. Now, here's the big mystery: if I execute the following SQL statements, everything is just fine, and works exactly as I expect:

SELECT IIF(1, 'true', 'false') FROM DUAL;
SELECT IIF(0, 'false', 'true') FROM DUAL;

However, the following generates a really bizarre error from Oracle:

SELECT IIF((0 = 1), 'false', 'true') FROM DUAL;

That error is as follows:

ORA-00907: missing right parenthesis.

Clearly, that isn't the case. Would anyone happen to have an explanation for this little bit of bizarreness?

It's taking a whole lot of self-control at the moment to restrain myself from hurling the Oracle server out the window. Oracle seems rife with these kinds of inanities.

EDIT: Is there some kind of magic syntax I have to use to use an equality operator in a select statement?


Solution

  • Maybe you can explain what you are trying to do. I think you are looking for the CASE or DECODE functions, but I can't be sure. It seems like you are working against the grain of the SQL language for some reason.

    The error occurs because Oracle doesn't expect a relational operator in the select clause of the query:

    SQL> SELECT IIF(1>7, 0, 1) FROM DUAL;
    SELECT IIF(1>7, 0, 1) FROM DUAL
                *
    ERROR at line 1:
    ORA-00907: missing right parenthesis
    
    SQL> SELECT 1=0 FROM DUAL;
    SELECT 1=0 FROM DUAL
            *
    ERROR at line 1:
    ORA-00923: FROM keyword not found where expected
    

    See this Ask Tom article.