CTE中')'附近的语法不正确

时间:2019-04-25 14:52:46

标签: sql sql-server

我写了以下CTE(公用表表达式)

WITH PRODUCTION_CTE(ShortProdNo,BoatRefNumber,ProdNo, CustomerPoNumber,LoadDate, Trailer, VeadaBuilding)
AS 
(
    SELECT 
    FBS.BoatNumber AS ShortProdNo,
    UOD.BoatRefNumber AS BoatRefNumber,
    FBS.ProdNo AS ProdNo,
    UOD.CustomerPoNumber AS CustomerPoNumber,
    FBS.Shipped AS LoadDate, 
    FBS.TruckNum AS Trailer,
    (CASE 
    WHEN Rtrim(UOD.CustomerPoNumber)='VEADA-VS1' THEN  'Bldg10'
     ELSE 'Bldg4'
     END) AS VeadaBuilding
FROM SysproCompanyV.dbo.FlatBenningtonShipping as FBS
INNER JOIN SysproCompanyV.dbo.UsrOrderDetails as UOD
ON FBS.BoatNumber=UOD.BoatRefNumber)

我从上述CTE中收到以下错误消息:

Msg 102, Level 15, State 1, Line 17
Incorrect syntax near ')'.

我不确定为什么会发生这种情况,因为CTE中允许内部联接,所有括号都已关闭并且名称已正确声明。

1 个答案:

答案 0 :(得分:6)

您需要在声明后立即调用CTE(即PRODUCTION_CTE):

;with PRODUCTION_CTE as (
      . . . 
)

select pc.*
from PRODUCTION_CTE pc