简体   繁体   English

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

[英]Incorrect syntax near the keyword 'SELECT'. Incorrect syntax near ')'

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. 这段代码工作正常,但是sql web。 Sheet gives the following error: Sheet提供以下错误:

Incorrect syntax near the keyword 'SELECT'. 关键字'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. 在SQLFiddle上演示。

This is what you are missing: 这就是你所缺少的:

1) Give an alias Table1 Query to a nested query. 1)将别名Table1 Query提供给嵌套查询。 The error says: It is not able to identify what [Table1 Query] is for. 错误说:它无法识别[Table1 Query]的用途。 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. 这是我对你的另一个问题的回答。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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