简体   繁体   English

SQL子查询语法错误附近')' - 不缺少别名

[英]SQL Subquery Syntax Error Near ')' — Not missing alias

I have a similar problem to this one , but the solution in that case was to add an alias to the subquery. 我有一个类似的问题这一个 ,但在这种情况下,解决办法是添加别名的子查询。 In my case, each of my subqueries has an alias, but I'm getting the message Incorrect syntax near ')' at the end of each of my subqueries. 在我的例子中,我的每个子查询都有一个别名,但是我在每个子查询的末尾都收到了Incorrect syntax near ')'

I'm writing the query in 2008 R2 but targeting SSRS 2000 我在2008 R2中编写查询但是以SSRS 2000为目标

My entire query is fairly long, but here's a shortened pseudo-code version: 我的整个查询相当长,但这是一个缩短的伪代码版本:

SELECT  A
       ,B
       ,C
       ,D
       ,E

FROM
     (
       SELECT A as 'A', B as 'B', id
       FROM table t
       WHERE A = 'some value'
       GROUP BY A, B
     ) AS sub1

LEFT JOIN 
     (
       SELECT C as 'C', D as 'D', id
       FROM 
            (
               SELECT id
               FROM nutherTable
               WHERE id IN
               (
                   SELECT DISTINCT id
                   FROM sub1
               )
            )
       WHERE D like '%param%'
     ) AS sub2

    ON sub2.id = sub1.id

LEFT JOIN
     (
       SELECT E as 'E', id
       FROM finalTable
       WHERE E IS NOT NULL
     ) AS sub3

     ON sub3.id = sub2.id

You'll notice that in the first LEFT JOIN , the join uses a subquery, and the FROM and WHERE clauses within that subquery also use subqueries. 您会注意到,在第一个LEFT JOIN ,连接使用子查询,该子查询中的FROMWHERE子句也使用子查询。 In my actual query, both LEFT JOINs have this same structure. 在我的实际查询中,两个LEFT JOINs都具有相同的结构。 One of the things I don't understand is that the nested subqueries don't require an alias. 我不明白的一件事是嵌套子查询不需要别名。 If I try to use an alias with the nested clauses, I get an error. 如果我尝试使用嵌套子句的别名,我会收到错误。 So it's only having trouble with the outer queries in the LEFT JOINs . 所以它只是在LEFT JOINs中遇到外部查询问题。

I've read in other posts that subqueries can only return results for a single field, but I've seen many example where multiple fields are returned from a subquery, so I don't think that's the problem here. 我在其他帖子中读过子查询只能返回单个字段的结果,但是我看过很多例子,从子查询返回多个字段,所以我不认为这是问题。 Or if that's going to be a problem, the error will be different. 或者,如果这将是一个问题,错误将是不同的。 Everything I've read attributes this problem to lacking an alias for the subquery, and I get the same results with or without an alias (no alias at all, as well as using AS and not using AS ). 我读过的所有内容都将此问题归结为缺少子查询的别名,并且无论是否使用别名(没有别名,以及使用AS而不使用AS ),我都会得到相同的结果。

You need to provide a table alias for every subquery you're using as a result set: 您需要为作为结果集使用的每个子查询提供表别名:

LEFT JOIN 
     (
       SELECT C as 'C', D as 'D', id
       FROM 
            (
               SELECT id
               FROM nutherTable
               WHERE id IN
               (
                   SELECT DISTINCT id
                   FROM sub1
               )
            ) SomeTableName -- HERE IS THE PROBLEM
       WHERE D like '%param%'
     ) AS sub2

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

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