48
Views
3
Comments
GetDate() is not supported in SQL Query variable assignment
Application Type
Reactive

Problem Summary:

  • Direct GETDATE() usage returns 2827 rows.
  • Using a declared @currentdate variable in the query (like DECLARE @currentdate DATETIME = GETDATE()) causes an error (DATETIME is not a recognized CURSOR option).
  • Using CurrDateTime() as an input parameter results in only 40 rows.

Direct GETDATE() vs CurrDateTime():

  • GETDATE() returns the current date and time (e.g., 2024-12-23 15:30:00).
  • CurrDateTime() in OutSystems also returns the current date and time, but it might behave differently than GETDATE() depending on its internal handling (for instance, if it's rounding or formatting the time differently).
  • The discrepancy in row counts (2827 vs 40) suggests that CurrDateTime() might be handling the time portion or its comparison in a way that's more restrictive or different from GETDATE(). 


  How can I achieve above use case while declaring once using it multiple time , as i dont want to write GetDate() in each comparison as it is causing performance issues


below is the where clasue where i want to use: 

WHERE     {TaxRuleVersion}.[IsLatestRevision]=1     AND ((@IsActiveVersion = 'True' AND ({TaxRuleVersion}.[EffectiveFrom] <=  GETDATE() AND {TaxRuleVersion}.[EffectiveTo] >= GETDATE()))      OR (@IsFutureVersion = 'True' AND {TaxRuleVersion}.[EffectiveFrom] > GETDATE())      OR (@IsEndedVersion = 'True' AND {TaxRuleVersion}.[EffectiveTo] < GETDATE()))


2024-12-02 13-16-47
Vipin Yadav

Hi @Bhushan Lahane,

To make your query more efficient and to stop repeating the use of GETDATE(), you can create a variable for it just once and use it every time you need it in your OutSystems query. The error you saw, "DATETIME is not a recognized CURSOR option," means that SQL requires variables to be declared and used properly. 

SQL -

DECLARE @CurrentDate DATETIME; 

SET @CurrentDate = GETDATE();

 SELECT  *  FROM {TaxRuleVersion}

 WHERE {TaxRuleVersion}.[IsLatestRevision] = 1  

AND (   

 (@IsActiveVersion = 'True' 

AND {TaxRuleVersion}.[EffectiveFrom] <= @CurrentDate 

AND {TaxRuleVersion}.[EffectiveTo] >= @CurrentDate)    

OR    (@IsFutureVersion = 'True' AND {TaxRuleVersion}.[EffectiveFrom] > @CurrentDate)    

OR    (@IsEndedVersion = 'True' AND {TaxRuleVersion}.[EffectiveTo] < @CurrentDate)  ) 


  • It sets the date once and uses it many times, which is better for performance.
  • It stops the system from recalculating GETDATE() every time it's compared, which cuts down on extra work.
  • You can use this method in OutSystems by putting the SQL inside an 'Advanced SQL' block.

Thanks,

Vipin Yadav

UserImage.jpg
Nani

Hi, @Bhushan Lahane 

Usually it's doesn't work like that both CurrDateTime() and GetDate() will be same, i don't know why your results are different.

Well you already have Variable to SQL called CurrentDate AS DateTime, So you can directly set a value inside a SQL even if you send value from logic that value will be override in SQL.

SET @CurrDateTime = GETDATE();

SELECT {TaxRuleVersion}.*

FROM {TaxRuleVersion}

WHERE   

         {TaxRuleVersion}.[IsLatestRevision] = 1
         AND ((@IsActiveVersion = 'True'
         AND ({TaxRuleVersion}.[EffectiveFrom] <=  @CurrDateTime
         AND {TaxRuleVersion}.[EffectiveTo] >= @CurrDateTime))   
 OR (@IsFutureVersion = 'True' AND {TaxRuleVersion}.[EffectiveFrom] > @CurrDateTime) OR (@IsEndedVersion = 'True' AND {TaxRuleVersion}.[EffectiveTo] < @CurrDateTime))


2021-11-12 04-59-31
Manikandan Sambasivam

Hi,

Retrieve the GETDATE() value, assign it to a local variable, and then use the variable in the SQL query.

 CurrentDate  = GETDATE();


WHERE     {TaxRuleVersion}.[IsLatestRevision] = 1    AND (        (IsActiveVersion = 'True'          AND ({TaxRuleVersion}.[EffectiveFrom] <= CurrentDate AND {TaxRuleVersion}.[EffectiveTo] >= CurrentDate))         OR (IsFutureVersion = 'True'             AND {TaxRuleVersion}.[EffectiveFrom] > CurrentDate)         OR (IsEndedVersion = 'True'             AND {TaxRuleVersion}.[EffectiveTo] < CurrentDate)    )

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.