我在这个问题上苦苦思索了几个小时,搜索了谷歌,但一无所获..
我正在尝试使用每行中数字(2,3,4)的循环序列来更新SQL Server表。
update table
set fieldname = (*black magic*)
where otherfieldname = something所以结果将会是
other fields|2|other fields|
other fields|3|other fields|
other fields|4|other fields|
other fields|2|other fields|
other fields|3|other fields|
other fields|4|other fields|
other fields|2|other fields|
other fields|3|other fields|欢迎任何想法!
干杯
发布于 2014-09-04 02:23:52
您可以使用row_number生成升序序列号。取mod 3得到一个介于0和2之间的数字,然后再加上2,使其介于2和4之间。
WITH T
AS (SELECT *,
( ROW_NUMBER()
OVER ( ORDER BY @@SPID) % 3 ) + 2 AS X
FROM YourTable)
UPDATE T
SET YourCol = X 发布于 2014-09-04 02:33:32
Ntile(limit)将为您的表提供均匀的数字分布。其中'limit‘是应该有多少个不同的值。只需将偏移量加1即可。
with t1 as (
select *, blackMagic = ntile(3) over(order by newid()) + 1
from someTable
)
update t1
set fieldname = blackMagichttps://stackoverflow.com/questions/25651073
复制相似问题