I am new in using OPENQUERY and I have encountered a problem when I use it inside a table-valued function, below is my query
INSERT INTO @returnList
SELECT *
FROM OPENQUERY(lnk_db, 'SELECT s.posting_date -COALESCE(max(a.due_date::date), l.grant_date) as loan_age
FROM ln.loan_snapshot_active s
INNER JOIN ln.loan_active l ON l.id = s.loan_id
LEFT JOIN ln.amortization_schedule a ON a.loan_id = l.id
AND a.status = ''PAID''
AND a.due_date < s.posting_date
WHERE s.posting_date = ''2020-01-07''::date
GROUP BY l.id, s.posting_date, l.grant_date');
It works well when I just put the date directly in where s.posting_date = ''2020-01-07''::date
but when I approach it dynamically I always encountered an error.
I have to pass the date chosen by the users and I do it this way
WHERE s.posting_date = ''''' +@postingdate+'''''::date
but I always encountered an error. (In this code I encountered an error stating Incorrect syntax near '+'.
)
Can anyone help me with this? I already tried the other solution but it never works.
Thanks in advance