The following SQL query is giving below error when run against a Microsoft SQL Server database from C#:
Incorrect syntax near the keyword JOIN
Certainly, the query contains the columns mentioned in the SELECT portion.
SELECT
Patient.FirstName,
Patient.LastName,
Patient.PatientId,
Patient.PatientSer,
CAST(Course.StartDateTime AS DateTime),
Course.ClinicalStatus,
CAST(Course.CompletedDateTime AS DateTime),
CAST(ScheduledActivity.CreationDate AS DateTime),
CAST(ScheduledActivity.ActualStartDate AS DateTime),
CAST(ScheduledActivity.ActualEndDate AS DateTime),
ScheduledActivity.ActivityNote,
Activity.ActivityCode,
Activity.ActivityType,
Activity.ObjectStatus,
CAST(Activity.HstryDateTime AS DateTime),
Activity.HstryTaskName
FROM
Patient
INNER JOIN Course
ON (Patient.PatientSer=Course.PatientSer)
INNER JOIN ScheduledActivity
ON (Patient.PatientSer=ScheduledActivity.PatientSer)
INNER JOIN ActivityInstance
ON (ScheduledActivity.ActivityInstanceSer=ActivityInstance.ActivityInstanceSer)
INNER JOIN Activity
ON (Activity.ActivitySer=ActivityInstance.ActivitySer)
WHERE
Patient.PatientId=@patientId;
A very similar query is successfully parsed without any issues. I just can't for the life of me see, why this isn't working. For now, I would like someone to just make sure that there actually is a syntax error in this query as the system claims, because I sure can't spot it.
There might also be an issue with the C# script that a "colleague" is using to run the query against the database, but that is a whole different issue.
System.Data.SqlClient.SqlConnection. The connection string given to the constructor of the class object is of the form"Server = <servername>; Database = <database>;Trusted_Connection = <bool>".c#for those experts to help. You may have issues with spaces and line breaks in formatting query.