[英]SQL - Incorrect syntax near “;”
I have an written a recursive SQL query which returns some int values. 我写了一个递归的SQL查询,它返回一些int值。 The SQL query looks like below: 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
This would return some integer values. 这将返回一些整数值。 (Works fine) What I want to do is that I want to write a query like below: (效果很好)我想做的是编写一个如下查询:
Select * from tExampleTable
WHERE FirstParameter IN (IntegerValuesHere) OR SecondParameter IN (IntegerValuesHere)
Where, IntegerValuesHere
are the values I get from the recursive query. 其中, IntegerValuesHere
是我从递归查询中获得的值。
The query now would look like: 现在查询如下所示:
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
)
But, I get an error which says that: 但是,我收到一条错误消息:
Incorrect syntax near ';' ';'附近的语法不正确 and Incorrect syntax near ')' 和')'附近的语法不正确
First, for the ;
首先,对于;
in front of WITH
. 在WITH
前面。 Second, for the )
before OR
. 其次,对于OR
之前的)
。 What am I missing? 我想念什么?
You cannot nest a CTE
like you are trying to do. 您不能像尝试那样嵌套CTE
。 You can use it like this: 您可以像这样使用它:
;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)
use data from different cte like this. 这样使用来自不同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
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.