您并未明确指出您正在使用的SQL Server版本,因此我将假设SQL Server 2005或更高版本。我相信WAITFOR DELAY命令将是一个不错的选项,可以为datetime列的每行添加1秒。
见这个例子:
-- Create temp table
CREATE TABLE #Client
(
RecordID int identity(1,1),
[Name] nvarchar(100) not null,
PurchaseDate datetime null
)
-- Fill in temp table with example values
INSERT INTO #Client
VALUES ('Jhon', NULL)
INSERT INTO #Client
VALUES ('Martha', NULL)
INSERT INTO #Client
VALUES ('Jimmy', NULL)
-- Create local variables
DECLARE @currentRecordId int,
@currentName nvarchar(100)
-- Create cursor
DECLARE ClientsCursor CURSOR FOR
SELECT RecordID,
[Name]
FROM #Client
OPEN ClientsCursor
FETCH FROM ClientsCursor INTO @currentRecordId, @currentName
-- Check @@FETCH_STATUS to see if there are any more rows to fetch.
WHILE @@FETCH_STATUS = 0
BEGIN
UPDATE #Client
SET PurchaseDate = DATEADD(ss,1,GETDATE())
WHERE RecordID = @currentRecordId
AND [Name] = @currentName
WAITFOR DELAY '00:00:01.000'
FETCH NEXT FROM ClientsCursor INTO @currentRecordId, @currentName
END
CLOSE ClientsCursor;
DEALLOCATE ClientsCursor;
SELECT *
FROM #Client
而这里的结果:
1 Jhon 2015-03-31 15:20:04.477
2 Martha 2015-03-31 15:20:05.473
3 Jimmy 2015-03-31 15:20:06.470
希望你找到这个答案有帮助
来源
2015-03-31 18:29:30
G21
您可以编写一个递增GETDATE值 – 2015-03-31 18:03:19
@JimSosa自定义函数雅这就是我想的......我在SSIS中这样做,所以这很容易。只是想看看是否有一个很酷的小窍门,我可以在查询中做到这一点 – 40Alpha 2015-03-31 18:04:58
你试过了DateAdd(ss,id,getDate())。假设你的ID都是1的增量 – 2015-03-31 18:07:22