exec('update #Temp set yhdzdID=2,'+@b+'=yhdzdID,id='+@b )

解决方案 »

  1.   

    update #Temp set yhdzdID=2,id=yhdzdID 
      

  2.   


    declare @b int 
    select @b=-1 
    update #Temp set yhdzdID=2
    update #Temp set @b=yhdzdID
    update #Temp set ID=@B
    select * from #Temp 
    这样试试看哦
      

  3.   

    yhdzdID=2有意义吗?
    结果弄成ID以全为2呀
      

  4.   

    yhdzdID=2  只是测试。 现在主要的问题是@b 没有得到yhdzdID 的值
      

  5.   

    可以的呀!@b得到了yhdzdid的值
    如下:
    if object_id('temp') is not null drop table temp
       select top 50 0 as id,100 as yhdzdID into temp
          from syscolumns a,syscolumns b  declare @b int 
      select @b=-1 
      update Temp set yhdzdID=2,@b=yhdzdID,id=@b  select * from Temp 
     
    --更新前的值
    0 100
    0 100
    0 100
    0 100
    0 100
    0 100
    0 100--更新后的值
    100 2
    100 2
    100 2
    100 2
    100 2
    100 2
    100 2
    100 2
      

  6.   

    楼主想说明什么问题?
    update语句,总是先执行变量的赋值再执行字段的赋值,所以
    update #Temp set yhdzdID=2,@b=yhdzdID,id=@b
    赋值次序是
    1)@b=yhdzdID 
    2) yhdzdID=2, id=@b
    所以id = 原yhdzdID字段值(而不是2)
      

  7.   

    #temp里没有数据怎么更新,当然都是null啦
      

  8.   

    update #Temp set yhdzdID=2,@b=yhdzdID,id=@b 這條一句是一次性更新的
    那有更新變量值的?
      

  9.   

    declare @b int 
    select @b=-1 
    update #Temp set yhdzdID=2,@b=yhdzdID,id=@b 
    此條語句中的@b=yhdzdid沒有更新前的值,如果沒更新前是空值,那當然就是空值啦!
    你雖@b=-1啦,但再語句中又賦啦一次把前邊給覆蓋啦!
      

  10.   

    create table test(b int)
    insert into test select 1
    insert into test select 1
    insert into test select 1
    insert into test select 1
    insert into test select 1
    insert into test select 1
    declare @a int
    set @a=0
    update test set @a=@a+1,b=@a
    select * from test
    go
    drop table test
    ------------------------------------
    create table test(b int)
    insert into test select 1
    insert into test select 1
    insert into test select 1
    insert into test select 1
    insert into test select 1
    insert into test select 1
    declare @a int
    set @a=0
    update test set b=@a,@a=@a+1    --區別
    select * from test
    go
    drop table test
      

  11.   

    这位大哥回答很是
    LZ可以试下就知道,你把对列yhdzdID=2放在外面,就是先赋值,然后
    update #Temp set @b=yhdzdID,id=@b 此时值都是2
      

  12.   

    实际上我的语句是这样的declare @b int 
    select @b=-1 
    update @Temp set 银行对账单ID=(select top 1 ID from @Temp2 where 借方金额1=借方金额 and 贷方金额1=贷方金额 and ID<>@b),@b=银行对账单IDselect * from #Temp 我是想在给银行对账单ID更新值的时候把@Temp2 里相同的记录排除掉;看来没办法这么做了
      

  13.   

    实际上我的语句是这样的 
    declare @b int 
    select @b=-1 
    Create  TABLE #Temp
    (
    日期 varchar(50),
    凭证编号 varchar(50),
    支票号 varchar(100),
    借方金额 decimal(18,2),
    贷方金额 decimal(18,2),
    余额 decimal(18,2),
    yhdzdID int,
    id int
    )declare @Temp2 TABLE
    (
    ID int,
    日 varchar(50),
    编号 varchar(50),
    借方金额1 decimal(18,2),
    贷方金额1 decimal(18,2),
    余额1 decimal(18,2)
    )
    update @Temp set 银行对账单ID=(select top 1 ID from @Temp2 where 借方金额1=借方金额 and 贷方金额1=贷方金额 and ID <>@b),@b=银行对账单ID select * from #Temp 
    我是想在给银行对账单ID更新值的时候把@Temp2 里相同的记录排除掉; 看来没办法这么做了
      

  14.   

    declare @b int 
    select @b=-1 
    Create  TABLE #Temp 

    日期 varchar(50), 
    凭证编号 varchar(50), 
    支票号 varchar(100), 
    借方金额 decimal(18,2), 
    贷方金额 decimal(18,2), 
    余额 decimal(18,2), 
    银行对账单ID int, ) declare @Temp2 TABLE 

    ID int, 
    日 varchar(50), 
    编号 varchar(50), 
    借方金额1 decimal(18,2), 
    贷方金额1 decimal(18,2), 
    余额1 decimal(18,2) 

    update @Temp set 银行对账单ID=(select top 1 ID from @Temp2 where 借方金额1=借方金额 and 贷方金额1=贷方金额 and ID <>@b),@b=银行对账单ID select * from #Temp 
    我是想在给银行对账单ID更新值的时候把@Temp2 里相同的记录排除掉; 看来没办法这么做了