【问题标题】:SQL - Incorrect syntax near ";"SQL - “;”附近的语法不正确
【发布时间】:2017-02-07 18:13:19
【问题描述】:

我编写了一个返回一些 int 值的递归 SQL 查询。 SQL 查询如下所示:

;WITH GroupHIERARCHY(ID)  
    AS  (  SELECT ID   
    FROM tFirstTable te  
    WHERE te.LevelID <> 0   
    AND GroupID =-1
    UNION ALL  
    SELECT t.ElementID  
    FROM tFirstTable AS t, tSecondTable,GroupHIERARCHY  
    WHERE t.TypeID=tSecondTable.TypeID  
    AND GroupHIERARCHY.ID= t.GroupID)

    SELECT ID FROM GroupHIERARCHY

这将返回一些整数值。 (工作正常) 我想做的是我想写一个如下查询:

Select * from tExampleTable 
WHERE FirstParameter IN (IntegerValuesHere) OR SecondParameter IN (IntegerValuesHere)

其中,IntegerValuesHere 是我从递归查询中得到的值。

查询现在看起来像:

Select * FROM tExampleTable 
    WHERE FirstParameter IN (
        ;WITH GroupHIERARCHY(ID)  
        AS  (  SELECT ID   
        FROM tFirstTable te  
        WHERE te.LevelID <> 0   
        AND GroupID =-1
        UNION ALL  
        SELECT t.ElementID  
        FROM tFirstTable AS t, tSecondTable,GroupHIERARCHY  
        WHERE t.TypeID=tSecondTable.TypeID  
        AND GroupHIERARCHY.ID= t.GroupID)

        SELECT ID FROM GroupHIERARCHY
        ) 

    OR SecondParameter IN (
        ;WITH GroupHIERARCHY(ID)  
        AS  (  SELECT ID   
        FROM tFirstTable te  
        WHERE te.LevelID <> 0   
        AND GroupID =-1
        UNION ALL  
        SELECT t.ElementID  
        FROM tFirstTable AS t, tSecondTable,GroupHIERARCHY  
        WHERE t.TypeID=tSecondTable.TypeID  
        AND GroupHIERARCHY.ID= t.GroupID)

        SELECT ID FROM GroupHIERARCHY
    )

但是,我收到一条错误消息:

';' 附近的语法不正确 和 ')' 附近的语法不正确

首先,对于WITH 前面的;。 其次,对于OR 之前的)。 我错过了什么?

【问题讨论】:

  • 只需删除 ;在 WITH 之前,他们两个。
  • @jarlh 这并不能解决问题。
  • @jarlh 删除后,我收到一条错误消息,提示靠近 WITH Expecting select。
  • 嗯,就是这样的ANSI SQL...但是你为什么不首先声明cte呢?
  • 引入CTE的关键字是WITH不是 ;WITH; 需要放在 statementend 处。但在这种情况下它不会有帮助,因为 SQL Server 不允许在子查询中使用 CTE。

标签: sql sql-server common-table-expression


【解决方案1】:

你不能像你试图做的那样嵌套CTE。你可以这样使用它:

;WITH GroupHIERARCHY(ID)  
    AS  (  SELECT ID   
    FROM tFirstTable te  
    WHERE te.LevelID <> 0   
    AND GroupID =-1
    UNION ALL  
    SELECT t.ElementID  
    FROM tFirstTable AS t, tSecondTable,GroupHIERARCHY  
    WHERE t.TypeID=tSecondTable.TypeID  
    AND GroupHIERARCHY.ID= t.GroupID)    
Select * 
from tExampleTable 
WHERE FirstParameter IN (SELECT ID FROM GroupHIERARCHY) OR 
      SecondParameter IN (SELECT ID FROM GroupHIERARCHY)

【讨论】:

  • 这个CTE在另一个CTE里面,这种情况我该怎么办?
  • @eg16 您可以简单地嵌套它们,将它们一个接一个地放置,例如:;With FirstCTE AS (), SecondCTE AS ( SELECT * FROM FirstCTE JOIN .. ) ...
【解决方案2】:

像这样使用来自不同 cte 的数据。

;WITH GroupHIERARCHY(ID)  
AS  (  

    SELECT ID   
    FROM tFirstTable te  
    WHERE te.LevelID <> 0   
    AND GroupID =-1
    UNION ALL  
    SELECT t.ElementID  
    FROM tFirstTable AS t, tSecondTable,GroupHIERARCHY  
    WHERE t.TypeID=tSecondTable.TypeID  
    AND GroupHIERARCHY.ID= t.GroupID
),
GroupHIERARCHY1(ID)  
AS  (  

    SELECT ID   
    FROM tFirstTable te  
    WHERE te.LevelID <> 0   
    AND GroupID =-1
    UNION ALL  
    SELECT t.ElementID  
    FROM tFirstTable AS t, tSecondTable,GroupHIERARCHY  
    WHERE t.TypeID=tSecondTable.TypeID  
    AND GroupHIERARCHY.ID= t.GroupID

)


SELECT ID FROM GroupHIERARCHY
Select * FROM tExampleTable 
WHERE FirstParameter IN (
select Id from GroupHIERARCHY
) 

OR SecondParameter IN (


SELECT ID FROM GroupHIERARCHY1
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多