SQL 关键字“SELECT”附近的语法不正确。')' 附近的语法不正确

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20298201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 23:18:40  来源:igfitidea点击:

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

sqlsyntax

提问by user3051630

I used the sql code in vb.net

我在 vb.net 中使用了 sql 代码

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:

此代码工作正常,但 sql web. 工作表给出以下错误:

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

关键字“SELECT”附近的语法不正确。')' 附近的语法不正确。

please help me.

请帮我。

采纳答案by dasblinkenlight

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.

SQLFiddle 上的演示。

回答by Vishal Suthar

This is what you are missing:

这就是你所缺少的:

1) Give an alias Table1 Queryto 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.

1)Table1 Query为嵌套查询提供别名。错误说:它无法确定[Table1 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];

回答by Amir Keshavarz

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.

这是我对你另一个问题的回答。