SQL - 错误的语法错误'with'

时间:2011-03-03 19:07:20

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

您好,请您帮我解决这个问题,我正在使用SQL Server 2005,我在“关键字'WITH'附近找到”错误的语法“错误,谢谢

create table #Act(
iAId int, 
iPAID int, 
sResource nvarchar(10), 
sName nvarchar(50), 
W1 int, 
W2 int, 
W3 int)

    INSERT INTO #Act (iAId, iPAID, sResource, sName, W1, W2, W3)
        SELECT AC00000.iAId, AC00000.iPAID, AC00000.sResource, 
               Activities.sName, WK00000.W1, WK00000.W2, WK00000.W3
        FROM   AC00000 INNER JOIN
               Activities ON AC00000.iActTypeId = Activities.iActivityId INNER JOIN
               WK00000 ON AC00000.iAId = WK00000.iAId
    ;WITH acts (iAId, iPAID, sResource, sName, W1, W2, W3) AS
    (
         SELECT parent.iAId, 
                parent.iPAID,
                parent.sResource, 
                parent.sName, 
                parent.W1, 
                parent.W2, 
                parent.W3
         FROM   #Act parent
         WHERE iPAID is null

         union all

         SELECT child.iAId, 
                child.iPAID,
                child.sResource, 
                child.sName, 
                child.W1, 
                child.W2, 
                child.W3
         FROM   #Act child

        join acts
        on child.iPAID = acts.iAID
    )
    select * from acts

1 个答案:

答案 0 :(得分:4)

WITH (Common table expressions)仅适用于SQL Server 2005或更高版本。

即使在兼容级别80,WITH语句也可以在SQL Server 2005中使用。

  • 检查您是否未针对SQL Server 2000实例运行SSMS 2005。
  • 检查您的服务器是否只有一个SQL Server实例,它是2005实例。
  • 运行此检查

select @@version

<小时/> 当您对SQL Server 2000

使用“with”时,会出现确切的错误文本
  

Msg 156,Level 15,State 1,Line 2
  关键字'with'附近的语法不正确。