每 5 秒插入 table 个连续的 DateTime 值
Insert into table consecutive DateTime values every 5 secounds
假设我有一个像这样的 table :
CREATE TABLE table1
(
CreatedAt DAteTimeOffset NULL
);
如何在 while
循环中插入 table 500 行并让每个日期每 5 秒一次?我希望我的结果是这样的:
2018-10-08 05:00:00.0000000 +00:00
2018-10-08 05:00:05.0000000 +00:00
2018-10-08 05:00:10.0000000 +00:00
2018-10-08 05:00:15.0000000 +00:00
2018-10-08 05:00:20.0000000 +00:00
2018-10-08 05:00:25.0000000 +00:00
2018-10-08 05:00:30.0000000 +00:00
2018-10-08 05:00:35.0000000 +00:00
2018-10-08 05:00:40.0000000 +00:00
2018-10-08 05:00:45.0000000 +00:00
2018-10-08 05:00:50.0000000 +00:00
2018-10-08 05:00:55.0000000 +00:00
2018-10-08 05:01:00.0000000 +00:00
2018-10-08 05:01:05.0000000 +00:00
2018-10-08 05:01:10.0000000 +00:00
2018-10-08 05:01:15.0000000 +00:00
等等...
我这里有一个 while 循环,但我不知道如何实现每 5 sek 插入具有值的连续行。
DECLARE
@i int = 0
WHILE @i < 500
BEGIN
INSERT INTO table1
(CreatedAt)
VALUES
(?)
END
尝试使用基于集合的方法。它通常要快得多:
WITH N AS --generate 500 rows (1..500)
(
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) N
FROM (VALUES (1),(2),(3),(4),(5)) A(A)
CROSS JOIN (VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9),(10)) B(B)
CROSS JOIN (VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9),(10)) C(C)
)
INSERT INTO table1 SELECT DATEADD(SECOND, (N-1)*5, SYSDATETIME()) FROM N
如果你真的非常需要一个循环(气馁),你可以使用下面的方法:
DECLARE @i int = 0;
DECLARE @d DAteTimeOffset = SYSDATETIME();
WHILE @i<500
BEGIN
INSERT table1 VALUES (@d);
SET @d = DATEADD(second, 5, @d);
SET @i += 1;
END
我认为 Zohar 的意思是这个解决方案
declare @t table (CreatedAt datetime)
insert into @t
select top 500
dateadd(second, (row_number() over (order by @@spid ) - 1) * 5, sysdatetime())
from sys.objects
--cross join sys.objects a cross join sys.objects b
select * from @t
假设我有一个像这样的 table :
CREATE TABLE table1
(
CreatedAt DAteTimeOffset NULL
);
如何在 while
循环中插入 table 500 行并让每个日期每 5 秒一次?我希望我的结果是这样的:
2018-10-08 05:00:00.0000000 +00:00
2018-10-08 05:00:05.0000000 +00:00
2018-10-08 05:00:10.0000000 +00:00
2018-10-08 05:00:15.0000000 +00:00
2018-10-08 05:00:20.0000000 +00:00
2018-10-08 05:00:25.0000000 +00:00
2018-10-08 05:00:30.0000000 +00:00
2018-10-08 05:00:35.0000000 +00:00
2018-10-08 05:00:40.0000000 +00:00
2018-10-08 05:00:45.0000000 +00:00
2018-10-08 05:00:50.0000000 +00:00
2018-10-08 05:00:55.0000000 +00:00
2018-10-08 05:01:00.0000000 +00:00
2018-10-08 05:01:05.0000000 +00:00
2018-10-08 05:01:10.0000000 +00:00
2018-10-08 05:01:15.0000000 +00:00
等等...
我这里有一个 while 循环,但我不知道如何实现每 5 sek 插入具有值的连续行。
DECLARE
@i int = 0
WHILE @i < 500
BEGIN
INSERT INTO table1
(CreatedAt)
VALUES
(?)
END
尝试使用基于集合的方法。它通常要快得多:
WITH N AS --generate 500 rows (1..500)
(
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) N
FROM (VALUES (1),(2),(3),(4),(5)) A(A)
CROSS JOIN (VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9),(10)) B(B)
CROSS JOIN (VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9),(10)) C(C)
)
INSERT INTO table1 SELECT DATEADD(SECOND, (N-1)*5, SYSDATETIME()) FROM N
如果你真的非常需要一个循环(气馁),你可以使用下面的方法:
DECLARE @i int = 0;
DECLARE @d DAteTimeOffset = SYSDATETIME();
WHILE @i<500
BEGIN
INSERT table1 VALUES (@d);
SET @d = DATEADD(second, 5, @d);
SET @i += 1;
END
我认为 Zohar 的意思是这个解决方案
declare @t table (CreatedAt datetime)
insert into @t
select top 500
dateadd(second, (row_number() over (order by @@spid ) - 1) * 5, sysdatetime())
from sys.objects
--cross join sys.objects a cross join sys.objects b
select * from @t