gpt4 book ai didi

sql - CTE 查询 SQL 服务器中的解析错误

转载 作者:行者123 更新时间:2023-12-02 07:23:55 25 4
gpt4 key购买 nike

我已经编写了一个 CTE 查询,并且正在 Microsoft SQL Server 2008 R2 Management Studio 中执行该查询:

WITH DependencyHierarchy(processName, dependProcessName) AS
(
SELECT
processName,
dependProcessName,
1 as HierarchyLevel
FROM processDependency

UNION ALL

SELECT
e.processName,
e.dependProcessName,
eh.HierarchyLevel + 1 AS HierarchyLevel
FROM
processDependency e
INNER JOIN
DependencyHierarchy eh ON e.dependProcessName = eh.processName
)
SELECT *
FROM DependencyHierarchy
ORDER BY HierarchyLevel, processName, dependProcessName;
GO

它抛出这个错误:

There was an error parsing the query. [ Token line number = 1,Token line offset = 1,Token in error = WITH ]

表格有这个数据:

    processName dependProcessName
P1 P2
P2 P3
P3 P4
P4 P5
P6 P7

最佳答案

WITHcommon_table_expression:

column_name

Specifies a column name in the common table expression. Duplicate names within a single CTE definition are not allowed. The number ofcolumn names specified must match the number of columns in the resultset of the CTE_query_definition.

The list of column names is optionalonly if distinct names for all resulting columns are supplied in thequery definition.

将列 HierarchyLevel 添加到 cte 列列表:

WITH DependencyHierarchy(processName,dependProcessName, HierarchyLevel)
AS
(
...
)

LiveDemo

或将其留空(列名将派生自第一个 SELECT):

WITH DependencyHierarchy AS
(
...
)

LiveDemo2

关于sql - CTE 查询 SQL 服务器中的解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36429695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com