我正在尝试使用另一个表中随机行的数据更新表中的每一行。这是我目前使用的 SQL:
SELECT Data, RowNumber FROM SampleData
SELECT FLOOR(ABS(CHECKSUM(NEWID())) / 2147483647.0 * 3 + 1) FROM Name
UPDATE Name SET Surname = (SELECT Data FROM SampleData WHERE RowNumber = FLOOR(ABS(CHECKSUM(NEWID())) / 2147483647.0 * 3 + 1))
这是我得到的结果:
Smith 1
Hunt 2
Jones 3
2
2
3
2
1
3
2
.... continues with a random number between 1 and 3 for each row in the Name table
Msg 512, Level 16, State 1, Line 9
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
所以我的问题是为什么 SELECT 语句在 UPDATE 子查询似乎返回多行的情况下每行产生一个随机数。我正在使用 SQL Server 2012 SP1,以防万一。