I got a query used by a Webservice which contains a subquery. When executing the query in SQL Server Management Studio, it works fine. However, using this in the webservice as a SqlCommand
, I get:
System.Data.SqlClient.SqlException: Incorrect Syntax near the keyword 'JOIN'. Incorrect Syntax near the keyword 'AS'.
Inserting extra characters let me figure out that the error refers to the position right after the subquery.
The query is as following:
SELECT H.H_ID AS ID, H.Name, TD.TDValue, A.A_ID, A_C.Value
FROM
H CROSS JOIN
A JOIN
(SELECT TD.A_ID, TD.H_ID, MAX(cast(TD.Version AS bigint)) AS Version FROM TD GROUP BY TD.A_ID, TD.H_ID) AS TData ON TData.H_ID = H.H_ID AND TData.A_ID = A.A_ID LEFT JOIN
TD2 ON TD2.H_ID = H.H_ID AND TD2.A_ID = A.A_ID AND TD2.Version = TData.Version LEFT JOIN
A_C ON A_C.A_ID = A.A_ID AND cast(A_C.R AS CHAR) = cast(TD.TDValue AS CHAR)
WHERE (H.Info = 1);
The Subquery is needed to get the last Version of an entry of the table TD. If there are better ways to do that, feel free to let me know as well. :-)
Are there any limitations with the SqlCommand in C# that do not exist in MS SQL, or am I doing something wrong?
I would appreciate any help so much!