根据行号更新值?

时间:2013-10-12 02:01:01

标签: sql-server tsql stored-procedures

我是SQL和存储过程的新手,我有点卡住了 - 所以任何帮助都会受到赞赏

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

这是我的Storedproc:

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

1 个答案:

答案 0 :(得分:1)

如果你只需要随机分配0或1 - 你可以使用RAND()和随机种子:

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

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