with as 子查询报错,请来看看是什么有关问题
with as 子查询报错,请来看看是什么问题SELECT WN_ID FROM (WITH project AS(SELECT WN_ID,WN_ParentID FR
with as 子查询报错,请来看看是什么问题
SELECT WN_ID FROM (
WITH project AS
(
SELECT WN_ID,WN_ParentID FROM LZMISPM.dbo.PM_WbsNode WHERE WN_ID='WN120905000031'
UNION ALL
SELECT a.WN_ID,a.WN_ParentID FROM LZMISPM.dbo.PM_WbsNode a JOIN project b ON a.WN_ParentID=b.WN_ID
)
SELECT WN_ID FROM project
)
这个语句报错,但是单独执行from里面的with ..as..select.. 语句是没有问题的。请问是什么原因?
错误信息是:
消息 156,级别 15,状态 1,第 4 行
关键字 'WITH' 附近有语法错误。
消息 319,级别 15,状态 1,第 4 行
关键字 'with' 附近有语法错误。如果此语句是公用表表达式或 xmlnamespaces 子句,那么前一个语句必须以分号结尾。
消息 102,级别 15,状态 1,第 11 行
')' 附近有语法错误。
[解决办法]WITH project AS
(
SELECT WN_ID,WN_ParentID FROM LZMISPM.dbo.PM_WbsNode WHERE WN_ID='WN120905000031'
UNION ALL
SELECT a.WN_ID,a.WN_ParentID FROM LZMISPM.dbo.PM_WbsNode a JOIN project b ON a.WN_ParentID=b.WN_ID
)
SELECT a.FAV_Id AS fav_id,a.FA_Name,
SUM(CASE WHEN b.FI_Status=1 THEN 1 ELSE 0 END) AS 进行中,SUM(CASE WHEN b.FI_Status=3 THEN 1 ELSE 0 END) AS 暂停,SUM(CASE WHEN b.FI_Status=0 THEN 9 ELSE 0 END) AS 完成
FROM dbo.WF_Form_Appliation a JOIN dbo.WF_Form_Instance b ON a.FA_Id=b.FA_Id
WHERE b.FI_FKId IN (SELECT WN_ID FROM project)
GROUP BY FAV_Id,FA_Name