0

我对 SQL 和存储过程很陌生,我有点卡住了 - 所以任何帮助将不胜感激

我如何循环遍历每一行并为其分配我生成的随机值?

这是我的存储过程:

CREATE PROCEDURE StoredProc8
AS
BEGIN

DECLARE @total INT 
DECLARE @Count INT = 0
DECLARE @Random INT = 0
SELECT @total = COUNT(CustomerID) FROM Customers
WHILE(@Count<= @total)
BEGIN


SELECT @Random = 2 * RAND()
EXEC ('update Customers set col1= ' + @Random )
SELECT @Count = @Count+1
END
END
4

1 回答 1

1

如果您只是需要随机分配 0 或 1 - 您可以将 RAND() 与随机种子一起使用:

UPDATE Customers SET COL1 = RAND(CHECKSUM(NEWID()))*2

演示:http ://sqlfiddle.com/#!3/31699/9

于 2013-10-12T02:13:03.993 回答