Skip to main content
Level 2
Jan 21, 2026 9:24 PM UTC
Solved

IF statement not working when running in a query.

I am trying to create a IF clause and has something like this..

$$

 

BEGIN

 SET calculated_date=(SELECT MAX(timestamp) FROM dataset);
  
SET current_date= (SELECT now());
  
IF(TO_DATE(now())>TO_DATE(calculated_date)) THEN

 SET calculte_another_date=(SELECT Date_field from a dataset);
  
IF(TO_DATE(now())= TO_DATE(calculte_another_date)) THEN

DO this

 

END IF;

END IF;

END

$$

 

    This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
    SarthakBhAccepted solution
    Adobe Employee
    Jan 22, 2026 2:32 AM UTC

    5 replies

    Level 2
    Jan 21, 2026 9:39 PM UTC

    getting this error...ErrorCode: 58000 Batch query execution gets : [failed reason ErrorCode: 58000 Batch query execution gets : [Internal System Error [Unsupported Statement in Anonymous Block]]]

    Translate
    Parvesh_Parmar
    Community Advisor
    Community Advisor
    Jan 21, 2026 10:06 PM UTC

    Hi ​@sociocandy , 

     

    This error indicates that one of the statements used in the anonymous block is not supported by Query Service.

    Most likely, the issue is caused by this line:
    SET calculated_date = (SELECT MAX(timestamp) FROM dataset);

     

    Query Service does not allow dataset queries (such as `SELECT ... FROM <dataset>`) inside `SET` statements within an anonymous block. Only scalar expressions or metadata functions are supported there.

    To confirm this, you could try replacing this statement with a hard-coded or constant value and run the block again. If it works, that will confirm the root cause.

    You would then need to move the dataset logic (such as `MAX(timestamp)`) into a regular SELECT or CTAS query instead of using it inside the anonymous block.

    Hope this helps.

    Kind regards,
    Parvesh

    Translate
    Parvesh Parmar – Adobe Community Advisor https://www.linkedin.com/in/parvesh-parmar/
    Level 2
    Jan 22, 2026 3:31 PM UTC

    But I need those values to compare in IF statements and then do things based on the outcome. Is there any way I can compare dates and use in IF statement?

    Translate
    Parvesh_Parmar
    Community Advisor
    Community Advisor
    Jan 21, 2026 9:56 PM UTC

    Hello ​@sociocandy , 

     

    Could you please share the exact error message you are seeing (for example, syntax error, type error, or runtime error)?

    Based on the Adobe Query Service documentation, your anonymous block structure and IF statement format look correct:https://experienceleague.adobe.com/en/docs/experience-platform/query/sql/syntax?utm_source=chatgpt.com#set

    The issue is most likely related to the functions used in the conditions rather than the IF syntax itself — especially expressions like:

    TO_DATE(now())
    TO_DATE(calculated_date)

    I recommend running these expressions independently in Query Service.

    This will help verify whether the date conversions are working as expected and returning valid values.

    Let us know the exact error message or the output you are getting so we can narrow this down further.

    Kind regards,
    Parvesh
     

    Translate
    Parvesh Parmar – Adobe Community Advisor https://www.linkedin.com/in/parvesh-parmar/
    SarthakBhAdobe EmployeeAccepted solution
    Adobe Employee
    Jan 22, 2026 2:32 AM UTC