with t 
as( 
select   
     a.id,    
    (select sum([数量]) from test b where b.id<=a.ID) as [累加值] 
from   
     test a 

update test 
set [累加值]=t.[累加值] from t where test.ID=t.ID  运行上述语句提示“在关键字 'with' 附近有语法错误。”请各位帮忙看看到时是哪里有问题

解决方案 »

  1.   

    with t 
     as( 
     select   
          a.id,    
         (select sum([数量]) from test b where b.id<=a.ID) as [累加值] 
     from   
          test a 
     ) 
     update test 
     set [累加值]=t.[累加值] from test,t where test.ID=t.ID  
      

  2.   

    update test set [累加值]=t.[累加值] from test,t where test.ID=t.ID  
      

  3.   

    你数据库是2000 还是2005
    2000不支持with
      

  4.   

    ;
    WITH    t AS ( SELECT   a.id ,
                            ( SELECT    SUM([数量])
                              FROM      test b
                              WHERE     b.id <= a.ID
                            ) AS [累加值]
                   FROM     test a
                 )
        UPDATE  test
        SET     [累加值] = t.[累加值]
        FROM    t
        WHERE   test.ID = t.ID  
      

  5.   

    USE test
    GO---->生成表test
    --
    --if object_id('test') is not null 
    -- drop table test
    --Go
    --Create table test([id] smallint,[数量] smallint,[累加值] smallint)
    --Insert into test
    --Select 1,1,0
    --Union all Select 2,2,0
    --Union all Select 3,3,0
    --Union all Select 4,4,0
    --Union all Select 5,5,0;with t 
    as( 
    select   
         a.id,  
         累加值,
        (select sum([数量]) from test b where b.id<=a.ID) as [Sum_累加值] 
    from   
         test a 

    UPDATE t SET 累加值=Sum_累加值
    SELECT * FROM test/*
    id     数量     累加值
    ------ ------ ------
    1      1      1
    2      2      3
    3      3      6
    4      4      10
    5      5      15
    */
      

  6.   

    你数据库是2000 还是2005
    2000不支持with 我的是2000的,with只支持2005吗?
      

  7.   

    with 是2005及以后版本才支持的新关键词,2000中不支持
    建议使用表变量或者临时表来处理类似问题。
      

  8.   

    with前面加 ; 试试或者你是哪个版本的数据库(2000下不支持公用表with cte
    as 写法)
      

  9.   

    if object_id('test') is not null  
        drop table test 
    Go 
    Create table test([id] smallint,[数量] smallint,[累加值] smallint) 
    Insert into test 
    Select 1,1,0 
    Union all Select 2,2,0 
    Union all Select 3,3,0 
    Union all Select 4,4,0 
    Union all Select 5,5,0 select ID,数量,累加值=(select sum(数量) as 累加值 from test where ID <=a.ID)
     from test a 
     order by idID     数量     累加值         
    ------ ------ ----------- 
    1      1      1
    2      2      3
    3      3      6
    4      4      10
    5      5      15(所影响的行数为 5 行)
      

  10.   

    2000不支持方便又快捷的CTE语法
      

  11.   

    ;WITH T 
    AS
    (
    SELECT *
    FROM Sales
    )
    select *
    FROM T
    上面这个而不会报错
    ;WITH T 
    AS
    (
    SELECT *
    FROM Sales
    )会报错;