时间:2021-07-01 10:21:17 帮助过:32人阅读
在MSSQL中,有些商品会伪造一些评价,但是评价信息可能会集中在一天或几天内,现在需要把日期打散,平均分不到一个日期段内,那么这段代码就有用了。 无 DECLARE @EvId INT,@PiD INT;DECLARE Evaluate CURSOR FOR SELECT EvaluateId,ProductId FROM dbo.Produ
在MSSQL中,有些商品会伪造一些评价,但是评价信息可能会集中在一天或几天内,现在需要把日期打散,平均分不到一个日期段内,那么这段代码就有用了。 <无>DECLARE @EvId INT,@PiD INT; DECLARE Evaluate CURSOR FOR SELECT EvaluateId,ProductId FROM dbo.ProductEvaluate WHERE CreateTime > '2014-10-20 00:00:01'; OPEN Evaluate; FETCH NEXT FROM Evaluate INTO @EvId,@PiD; WHILE @@FETCH_STATUS = 0 BEGIN DECLARE @BDate datetime, @EDate DATETIME,@PDate DATETIME; SET @BDate = '20140101' --下限 SELECT @BDate = CONVERT(varchar(100), CreateTime, 112) FROM dbo.Products WHERE ProductId = @PiD; SET @EDate = '20141020 23:59:59' --上限 UPDATE dbo.ProductEvaluate SET CreateTime =DATEADD(second,ABS(CHECKSUM(NEWID()))%DATEDIFF(second,@BDate,@Edate),@BDate) WHERE EvaluateId = @EvId ; FETCH NEXT FROM Evaluate INTO @EvId,@PiD; END CLOSE Evaluate; DEALLOCATE Evaluate;