SQL Server 2012 Random string from a list

前端 未结 5 628
暖寄归人
暖寄归人 2021-01-01 18:20

say I have 3 values, Bill, Steve, Jack. and I want to randomly update a table with those values, eg

Update contacts set firstname = (\'Bill\',\'Steve\',\'Jack\') whe

5条回答
  •  一向
    一向 (楼主)
    2021-01-01 18:56

    You can do this with the following trick:

    update c set name=ca.name
    from contacts c
    outer apply(select top 1 name 
                from (values('bill'),('steve'),('jack')) n(name)
                where c.id = c.id order by newid())ca;
    

    c.id = c.id is just a dummy predicate that forces sql engine to call subquery for each outer row. Here is the fiddle http://sqlfiddle.com/#!6/8ecca/22

提交回复
热议问题