Hi
Incorrect syntax near Select
SQL
SELECT Sum(T1."SumApplied"),
(Select "Location" from OLCT where "Code" = (Select Max(A0."LocCode") from PCH1 A0 inner join OPCH A1 on A0."DocEntry" = A1."DocEntry"
where A0."DocEntry" In (Select "DocEntry" from VPM2 where "DocNum" = Select("CreatedBy" from OJDT where "TransId" = T0."TransId")))
) As "Location"
FROM OJDT T0 inner join VPM2 T1 On T1."DocNum" = T0."CreatedBy"Thanks

Eliana BlakeRank #1,595 · 993 reputationPosted Apr 25, 2025, 2:47 AM EDT
The error "Incorrect syntax near Select" in the SQL query you provided is due to a misplaced or misused "Select" statement within the subquery. Here's a breakdown of the issue and how you can address it:
In the SQL query you provided:
SELECT Sum(T1."SumApplied"), (Select "Location" from OLCT where "Code" = ( Select Max(A0."LocCode") from PCH1 A0 inner join OPCH A1 on A0."DocEntry" = A1."DocEntry" where A0."DocEntry" In ( Select "DocEntry" from VPM2 where "DocNum" = Select("CreatedBy" from OJDT where "TransId" = T0."TransId") ) ) ) As "Location" FROM OJDT T0 inner join VPM2 T1 On T1."DocNum" = T0."CreatedBy"The issue lies within this part:
"DocNum" = Select("CreatedBy" from OJDT where "TransId" = T0."TransId")To correct the syntax error, you need to adjust the subquery structure. The corrected subquery should have its own SELECT statement in parentheses and a comparison operator where needed. Here's a modified version of that part of the query:
"DocNum" = (Select "CreatedBy" from OJDT where "TransId" = T0."TransId")By adding parentheses around the subquery and ensuring the SELECT statement is correctly positioned, you should be able to address the syntax error "Incorrect syntax near Select" in your SQL query.
If you make this adjustment, the query should execute without syntax errors. Always remember that correct syntax is crucial in SQL queries to ensure they are valid and produce the desired results.