Problem Summary:
Direct GETDATE() vs CurrDateTime():
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()))
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) )
Thanks,
Vipin Yadav
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))
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) )