IF statement not working when running in a query. | Community
Skip to main content
Level 2
January 21, 2026
Question

IF statement not working when running in a query.

  • January 21, 2026
  • 3 replies
  • 38 views

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

$$

 

    3 replies

    Level 2
    January 21, 2026

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

    Parvesh_Parmar
    Community Advisor
    Community Advisor
    January 21, 2026

    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

    Parvesh Parmar – Adobe Community Advisor https://www.linkedin.com/in/parvesh-parmar/
    Level 2
    January 22, 2026

    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?

    Parvesh_Parmar
    Community Advisor
    Community Advisor
    January 21, 2026

    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
     

    Parvesh Parmar – Adobe Community Advisor https://www.linkedin.com/in/parvesh-parmar/
    Adobe Employee
    January 22, 2026