我有一个MyDatabase.MyTable.DateCol包含几千行,我想用日期时间填充它。我希望每个日期都比前一个日期大1秒。我怎样才能做到这一点?

最佳答案

样品表

CREATE Table DateTable
(ID INT IDENTITY(1,1),Name NVARCHAR(300), Data Datetime)
GO


测试数据

INSERT INTO DateTable (Name)
VALUES ('John'),('Mark'),('Phil'),('Simon'),('Sam'),('Pete'),('Josh')
GO


询问

;WITH CTE
AS
(
SELECT *, rn = ROW_NUMBER() OVER (ORDER BY ID ASC)  FROM DateTable
)
UPDATE CTE
SET  Data = DATEADD(SECOND, CTE.rn, GETDATE())


结果集

SELECT * FROM DateTable

ID  Name    Data
1   John    2013-11-06 20:34:59.310
2   Mark    2013-11-06 20:35:00.310
3   Phil    2013-11-06 20:35:01.310
4   Simon   2013-11-06 20:35:02.310
5   Sam     2013-11-06 20:35:03.310
6   Pete    2013-11-06 20:35:04.310
7   Josh    2013-11-06 20:35:05.310

关于sql - 填充表,日期时间每增加一秒,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19821823/

10-11 01:16