sql给字段循环赋随机值
DECLARE @PKey bigint --声明@PKey
DECLARE PKey_cursor CURSOR FOR SELECT PKey FROM tableName --声明游标
OPEN PKey_cursor
FETCH NEXT FROM PKey_cursor
INTO @PKey
WHILE @@FETCH_STATUS = 0 --获取游标
BEGIN
UPDATE
tableName
SET
fieldA = '编号'+CAST(PKEY AS VARCHAR) +'-' +CAST(FLOOR(RAND()*100) AS VARCHAR) -- 值 cast(floor(rand()*100) as varchar)是取100以内随机整数
WHERE
PKEY = @PKEY -- 改库
FETCH NEXT FROM PKey_cursor INTO @PKey
END
CLOSE PKey_cursor;
DEALLOCATE PKey_cursor;