简体   繁体   中英

Incorrect syntax near the keyword 'SELECT'. Incorrect syntax near ')'

I used the sql code in vb.net

SELECT [Table1 Query].[amel_code], [Table1 Query].[kala_code], Sum([Table1 Query].
[SumOfqty]) AS SumOfSumOfqty FROM(
SELECT Table1.amel_code,
       Table1.amani_code,
       Table1.kala_code,
       Sum(Table1.qty) AS SumOfqty
FROM Table1
GROUP BY Table1.amel_code,
         Table1.amani_code,
         Table1.kala_code HAVING (((Table1.amel_code)=[?]) AND ((Table1.amani_code)<[?]));
)
GROUP BY [Table1 Query].[amel_code], [Table1 Query].[kala_code];

This code is working properly but the sql web. Sheet gives the following error:

Incorrect syntax near the keyword 'SELECT'. Incorrect syntax near ')'.

please help me.

You need to remove semicolon at the end of the nested query, and add an alias to it:

SELECT [Table1 Query].[amel_code], [Table1 Query].[kala_code], Sum([Table1 Query].[SumOfqty]) AS SumOfSumOfqty
FROM (
SELECT Table1.amel_code,
       Table1.amani_code,
       Table1.kala_code,
       Sum(Table1.qty) AS SumOfqty
FROM Table1
GROUP BY Table1.amel_code,
         Table1.amani_code,
         Table1.kala_code
HAVING (((Table1.amel_code)=[?])
         AND ((Table1.amani_code)<[?])) -- ; <<== Remove this semicolon
) [Table1 Query] -- <<== Add this alias
GROUP BY [Table1 Query].[amel_code], [Table1 Query].[kala_code];

Demo on SQLFiddle.

This is what you are missing:

1) Give an alias Table1 Query to a nested query. The error says: It is not able to identify what [Table1 Query] is for. so you have to give that alias to a sub query.

SELECT [Table1 Query].[amel_code], [Table1 Query].[kala_code], Sum([Table1 Query].[SumOfqty]) AS SumOfSumOfqty 
FROM(
SELECT Table1.amel_code,
       Table1.amani_code,
       Table1.kala_code,
       Sum(Table1.qty) AS SumOfqty
FROM Table1
GROUP BY Table1.amel_code,
         Table1.amani_code,
         Table1.kala_code HAVING (((Table1.amel_code)=[?]) AND ((Table1.amani_code)<[?]))
) [Table1 Query]
GROUP BY [Table1 Query].[amel_code], [Table1 Query].[kala_code];

Every subquery or nested query should have alias. example:

SELECT *
FROM (SELECT * FROM T1 WHERE ID>50) D

This was my answer on your other question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM