skip to Main Content

This query below is running but I would like to add this extra condition; run this query only if there is data in the table STAGE.LAB.DATA.
I tried CASE and IF THEN, but I can’t get it to work. Please help. What syntax should be added? Thanks

SELECT *
 FROM
 ( SELECT EXISTS ( SELECT 1 FROM STAGE.LAB.DATA A
  WHERE (A.HOT_TEMP_VALUE >= 118 AND A.HOT_TEMP_VALUE <= 122) ) AS result)
   WHERE RESULT !='TRUE';

IF
 SELECT COUNT(*) FROM STAGE.LAB.DATA >1
 THEN
 SELECT *
 FROM
 ( SELECT EXISTS ( SELECT 1 FROM STAGE.LAB.DATA A
 WHERE (A.HOT_TEMP_VALUE >= 118 AND A.HOT_TEMP_VALUE <= 122) ) AS result)
 WHERE RESULT !='TRUE';
 ELSE
  END IF

2

Answers


  1. In order to use a query result as a value, you have to put it in parentheses:

    IF (SELECT COUNT(*) FROM STAGE.LAB.DATA) >= 1
    THEN 
        ...
    END IF
    

    IF statements can only be used in stored programs. In an ordinary query, you can use WHERE clause:

    SELECT *
    FROM otherTable
    WHERE EXISTS (SELECT 1 FROM STAGE.LAB.DATA)
    

    This will return an empty result if there’s nothing in STAGE.LAB.DATA.

    Login or Signup to reply.
  2. IF EXISTS (SELECT NULL FROM STAGE.LAB.DATA) THEN 
      ...
    END IF
    
    SELECT ...
      ...
    WHERE EXISTS (SELECT NULL FROM STAGE.LAB.DATA)
      ...
    

    EXISTS stops table scanning after first matched row is found. Rather than COUNT()..

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search