I have the following query which works perfectly fine:
Version #1
SELECT
t.ScheduleId,t.BaseDate,t.AfterDate,
fn.ScheduleDate AS NextBillingDate
INTO
#Distinct_BillableMemberAgreementItems
FROM
Distinct_BillableMemberAgreementItems_CTE t
CROSS APPLY
dbo.fn_ScheduleCalculator(t.ScheduleId, t.BaseDate, t.AfterDate, 0, 1, NULL, NULL, NULL) fn
Further, I did the following changes to the above query which also works fine:
Version #2
SELECT
t.ScheduleId, t.BaseDate, t.AfterDate, t.memberagreementitemid,
fn.ScheduleDate AS NextBillingDate
INTO
#Distinct_BillableMemberAgreementItems
FROM
Distinct_BillableMemberAgreementItems_CTE t
CROSS APPLY
dbo.fn_ScheduleCalculator(t.ScheduleId, t.BaseDate, COALESCE(t.PreviousBillingDate, t.BaseDate), 0, 2, NULL, NULL, NULL) fn
WHERE
fn.RowNumber = 2
The question
Now, based on a certain condition I want to pass different args in the user defined function dbo.fn_ScheduleCalculator
, but I am not able to get the syntax right.
Here's what I am trying to achieve:
SELECT
t.ScheduleId, t.BaseDate, t.AfterDate, t.memberagreementitemid,
fn.ScheduleDate AS NextBillingDate
INTO
#Distinct_BillableMemberAgreementItems
FROM
Distinct_BillableMemberAgreementItems_CTE t
CROSS APPLY
IF ((COALESCE(t.PreviousBillingDate, t.LastInvoicedDate) = GETDATE())
OR (t.BaseDate <> GETDATE() and t.FromBilling = 0))
dbo.fn_ScheduleCalculator(t.ScheduleId, t.BaseDate, COALESCE(t.PreviousBillingDate, t.BaseDate), 0, 2, NULL, NULL, NULL) fn
WHERE
fn.RowNumber = 2
ELSE
dbo.fn_ScheduleCalculator(t.ScheduleId, t.BaseDate, t.AfterDate, 0, 1, NULL, NULL, NULL) fn)
I understand that without sharing some data for the above tables it will be hard to suggest the exact syntax on how to achieve what I am looking for, but even if someone can suggest how I can achieve what I am looking for or if any issues in the syntax that you see and can point out will be of great help.
Here:
dbo.fn_ScheduleCalculator
: is a user defined function
Distinct_BillableMemberAgreementItems_CTE
: is a CTE