Вопрос

I need to return a value if select returned null. however I found a solution here by putting a query in a sub-query

SELECT COALESCE((SELECT id FROM tbl WHERE id = 9823474), 4) AS id FROM RDB$DATABASE;

The query above would return Null because the value 9823474 does not exist in the table but I want to return a value in that case (for ex 4) so I found the only solution to use select inside sub query and then COALESCE would work, If I did not do that COALESCE will also return Null.

Is it the only solution ?

Это было полезно?

Решение

No, that is not an only way for example

Select first 1 id from (
    Select id FROM tbl WHERE id = 9823474

    Union All

    Select 4 from rdb$database) 

Or you can use anonymous procedure http://firebirdsql.su/doku.php?id=execute_block

 EXECUTE BLOCK RETURNS ( id integer )
 AS
 BEGIN
      IF ( EXISTS (SELECT * FROM tbl WHERE id = 9823474) )
      THEN id = 9823474;
      ELSE id = 4;

      SUSPEND;
 END

... there always are many methods there

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top