declare @c char(20)
declare @c1 char(20)
declare @i money
declare @sql char(8000)select @c=医生,@c1=费用类别,@i=费用 from aa where [id]=1
select @c,@c1,@i
--以上是正确的.
--后面的错误请高手帮忙修改
sql='update sheet set '@c'='@c'+'@i' from sheet where 医生='@c''
exec sql

解决方案 »

  1.   

    sql='update sheet set '+@c+'='+@c+'+'+ltrim(@i)+' from sheet where 医生='''+@c+''''
    exec (sql)
      

  2.   

    set @sql='update sheet set 医生 = ''' + @c + ''',费用 = ' + rtrim(@i) + ' where 医生= ''' + @c + ''''
    exec(@sql)
      

  3.   

    靠,你错的地方可多了,update table set field=value where 1=1,怎么又一个from?
      

  4.   

    表A
    费用类别,费用,医生姓名 id 
    治疗费    100   aa      1
    B超费     200   bb      2
    打针费    300   cc      3
    ... 
    ...表B
    医生姓名, 治疗费,  B超费, 打针费,床位费  ... ...
    aa         100      100    100    100
    bb         200      300    100    200
    cc         300      200     200    200UPDATE后要出来的结果医生姓名, 治疗费,  B超费, 打针费,床位费  ... ...
    aa         200      100    100    100
    bb         200      500    100    200
    cc         300      200     500    200
    因为表A数据很多,最好有个循环做完
      

  5.   

    exec (sql)
    必须加括号才行呀!
      

  6.   

    http://community.csdn.net/Expert/topic/5607/5607205.xml?temp=.4086725
      

  7.   

    sql='update sheet set '+@c1+'='+@c1+'+'+ltrim(@i)+' from sheet where 医生='''+@c+''''
    exec (sql)
      

  8.   

    Create Table A
    (费用类别 Nvarchar(20),
     费用 Int,
     医生姓名 Varchar(20),
     id Int)
    Insert A Select   N'治疗费',    100,   'aa',      1
    Union All Select N'B超费',      200,   'bb',      2
    Union All Select N'打针费',    300,   'cc',      3Create Table B
    (医生姓名 Varchar(20),
     治疗费 Int,
     B超费 Int,
     打针费 Int,
     床位费 Int)
    Insert B Select 'aa',         100,      100,    100,    100
    Union All Select 'bb',         200,      300,    100,    200
    Union All Select 'cc',         300,      200,     200,    200
    GO
    Declare @S1 Nvarchar(4000), @S2 Nvarchar(4000)
    Select @S1 = '', @S2 =''
    Select @S1 = @S1 + ',' + 费用类别 + N' = B.' + 费用类别 + ' + C.' + 费用类别 , @S2 = @S2 + N', SUM(Case 费用类别 When N''' + 费用类别 + N''' Then 费用 Else 0 End) As ' + 费用类别
    From A Group By 费用类别
    Select @S1 = 'Update B Set ' + Stuff(@S1, 1, 1, '') + N' From B Inner Join (Select 医生姓名' + @S2 + N' From A Group By 医生姓名) C On B.医生姓名 = C.医生姓名'
    EXEC(@S1)
    GO
    Select * From B
    GO
    Drop Table A, B
    --Result
    /*
    医生姓名 治疗费 B超费 打针费 床位费
    aa 200 100 100 100
    bb 200 500 100 200
    cc 300 200 500 200
    */