如何用20個隨機選擇的值更新10000條記錄?如何用20個隨機選擇的值更新10000條記錄?
例如:在一張表中,我有像firstname,lastname,emailid這樣的字段,它有10000條記錄。我想用我測試的電子郵件ID列表更新emailid字段。
如何用20個隨機選擇的值更新10000條記錄?如何用20個隨機選擇的值更新10000條記錄?
例如:在一張表中,我有像firstname,lastname,emailid這樣的字段,它有10000條記錄。我想用我測試的電子郵件ID列表更新emailid字段。
下面是一個例子:
declare @EmailList table
(
EmailIndex int identity(1, 1) primary key,
EmailAddress nvarchar(100)
)
insert @EmailList (EmailAddress)
select '[email protected]' union all
select '[email protected]' union all
select '[email protected]'
-- other emails here...
declare @emailListLength int
select @emailListLength = count (*) from @EmailList
update Users set Email = el.EmailAddress
from
(select CEILING(RAND(CONVERT(varbinary, newid())) * @emailListLength) as randomEmailIndex, * from Users) rnd
join @EmailList el on el.EmailIndex = rnd.randomEmailIndex
的CEILING(RAND(CONVERT(varbinary, newid()))
表達會產生隨機數從1
到[email list length]
然後我們加入了用戶表使用該隨機指標預定義的枚舉電子郵件列表。
+1我喜歡你加入@EmailList的方式。我可能會嘗試row_number()OVER(ORDER BY校驗和(*))或類似的東西。 – eric 2012-02-27 10:42:32
感謝託梅克在原來的答案
UPDATE PersonalInformation
SET EmailID = ABS(CAST(NEWID() AS BINARY(6)) % 20)
你的意思是指出錯誤和冗餘更新20個行? – 2012-02-27 10:18:07
不,更新所有10000個行,隨機選擇20個值 – Pradeep 2012-02-27 10:20:02