I get the following error:
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
with this SQL:
SELECT DISTINCT
Name, t1.[value] AS DateTime
FROM
BmaCare.Questionnaire qu
LEFT JOIN
BmaCare.Pregnancy pr ON qu.PregnancyId = pr.Id
CROSS APPLY
OPENJSON(qu.Data, '$.actions')
WITH (
entries NVARCHAR(MAX) '$.entries' AS JSON) j
CROSS APPLY
OPENJSON(j.entries)
WITH (
[key] NVARCHAR(100) '$.key',
[value] NVARCHAR(100) '$.value') t1
WHERE
t1.[key] = 'Admission.DateTime' OR t1.[key] = 'Discharge.DateTime'
AND t1.[value] IS NOT NULL
AND pr.Uuid = '50E8835B-4ECE-4333-9B5B-DD6AF611989C'
ORDER BY
DateTime DESC
A solution for this has been given here: Incorrect syntax near the keyword 'with'. If this statement is a common table expression which amounts to preceding every WITH by a semicolon. Very simple. When I do so in my SQL string, I get
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near '('.
What do I do to get the query working?