本帖最后由 chirea 于 2011-12-29 17:29:32 编辑

解决方案 »

  1.   

    declare @sql nvarchar(max)
    select @sql='update '+表+' set '+字段+'='+值 from table
    exec(@sql)
      

  2.   


    declare @a table (id int,col varchar(1))
    insert into @a
    select 1,'a' union all
    select 2,'b' union all
    select 3,'c'declare @b table (id int,col varchar(1))
    insert into @b
    select 1,'d' union all
    select 2,'e' union all
    select 3,'f'--查询
    select * from @a
    /*
    id          col
    ----------- ----
    1           a
    2           b
    3           c
    */
    --更新
    update @b
    set col=a.col
    from @b b left join @a a on b.id=a.idselect * from @b
    /*
    id          col
    ----------- ----
    1           a
    2           b
    3           c
    */
      

  3.   

    declare @sql nvarchar(max)
    select @sql=isnull(@sql,'')+'update '+表+' set '+字段+'='+值+'; ' from table
    exec(@sql)
      

  4.   


    --如果值是字符型,还要加单引号
    declare @sql nvarchar(max)
    select @sql=isnull(@sql,'')+'update '+表+' set '+字段+'='''+ltrim(值)+'''; ' from table
    exec(@sql)