MSSQL 如何随机 UPDATE A表 某一行  某一个列的值为0

解决方案 »

  1.   

    -- 假定A表(ta)字段中有主键字段名为id, 数据库名字为test
    -- 取出非ID字段的其他随机字段名
    declare @coln varchar(50)
    select top 1 @coln=name from sys.SysColumns  
    where id=(SELECT id FROM test..SysObjects Where XType='U' and name='ta')
    and name<>'id'
    order by NEWID()
    -- 更新随机记录随机字段值为0
    exec ('update ta set '+ @coln +'=''0'' where id=(
    select top 1 id from ta
    order by NEWID() 
    )');
      

  2.   


    update table set col=newvalue 
    where id=cast(rand()*xxxx as int)