如何使用getdate()+ 20秒插入每一行

时间:2018-05-07 20:33:35

标签: sql-server

如何使用getdate()+ 20秒插入行 插入每行+ 20秒

实施例

insert into Student (id, Name, Mar,cTime)  
       select  id , Name , Mar , getdate() from sende

预期输出行+ 20秒

| id| Name| Mar |   cTime             |
---------------------------------------
| 1 | ha1 | 100 | 2018-05-07 22:49:00 |
| 2 | ha2 | 200 | 2018-05-07 22:49:20 |
| 3 | ha3 | 300 | 2018-05-07 22:49:40 |
| 4 | ha4 | 400 | 2018-05-07 22:50:00 |
| 5 | ha5 | 500 | 2018-05-07 22:50:20 |

5 个答案:

答案 0 :(得分:1)

只需使用dateadd函数:

declare @Student table  
(  
   id int ,  
   Name varchar(50),  
   Mar int ,
   cTime datetime 
)  

insert into @Student (id, Name, Mar,cTime)  
values(1,'ha1',100,getdate()),  
(2,'ha2',200, dateadd(s,20,getdate())),
(3,'ha3',300,dateadd(s,40,getdate())),  
(4,'ha4',400,dateadd(s,60,getdate())),
(5,'ha5',500,dateadd(s,80,getdate()))

select * from @Student 

答案 1 :(得分:1)

你可以手动方式:

SQL DEMO

DECLARE @date DATETIME
SET @date = GETDATE()

declare @Student table  
(  
   id int ,  
   Name varchar(50),  
   Mar int ,
   cTime datetime 
)  

insert into @Student (id, Name, Mar,cTime) values(1,'ha1',100, @date)
SET @date = DATEADD(ss, 20, @date)
insert into @Student (id, Name, Mar,cTime) values(2,'ha2',200, @date)
SET @date = DATEADD(ss, 20, @date)
insert into @Student (id, Name, Mar,cTime) values(3,'ha3',300, @date)
SET @date = DATEADD(ss, 20, @date)
insert into @Student (id, Name, Mar,cTime) values(4,'ha4',400, @date)
SET @date = DATEADD(ss, 20, @date)
insert into @Student (id, Name, Mar,cTime) values(5,'ha5',500, @date)
SET @date = DATEADD(ss, 20, @date)

SELECT * FROM @student

答案 2 :(得分:1)

有很多方法可以做到这一点。在插入过程中可能使用计数表或其他逻辑。但这是另一种方式。只需插入数据,然后使用ROW_NUMBER更新它。它在行数方面是动态的,不需要对原始插入语句以外的值进行硬编码。

declare @Student table  
(  
   id int ,  
   Name varchar(50),  
   Mar int ,
   cTime datetime 
)  

;
insert into @Student (id, Name, Mar,cTime)  
    values(1, 'ha1', 100, getdate()),  
    (2, 'ha2', 200, getdate()),  
    (3, 'ha3', 300, getdate()),  
    (4, 'ha4', 400, getdate()),
    (5, 'ha5', 500, getdate())  

select * from @Student; --so you can see the original values

with MyCTE as
(
    select *
        , RowNum = ROW_NUMBER() over (order by id) - 1
    from @Student
)

update @Student
set cTime = dateadd(second, 20 * RowNum, c.cTime)
from MyCTE c;

select * from @Student; --every row is now 20 seconds greater than the row "before it"

答案 3 :(得分:1)

与Sean回答类似,但不是更新,只需计算插入前的日期。

<强> SQL DEMO

CREATE TABLE sende
    ([id] int, [Name] varchar(3), [Mar] int)
;

INSERT INTO sende
    ([id], [Name], [Mar])
VALUES
    (1, 'ha1', 100),
    (2, 'ha2', 200),
    (3, 'ha3', 300),
    (4, 'ha4', 400),
    (5, 'ha5', 500)
;

With cte as (
    SELECT *, row_number() over ( ORDER BY [id]) -1 as rn
    FROM sende
)    
SELECT *, DATEADD(ss, rn * 20, GETDATE()) as cTime
FROM cte

答案 4 :(得分:0)

以这种方式插入数据并没有多大意义,而且您不会以某种方式从应用程序中插入数据。此外,每行需要20秒。当你为数千行执行此操作时,它会变得混乱。那么,为什么不在SELECT

上处理它
declare @Student table  
(  
   id int ,  
   Name varchar(50),  
   Mar int ,
   cTime datetime 
)  

insert into @Student (id, Name, Mar)  
values
(1,'ha1',100),  
(2,'ha2',200),  
(3,'ha3',300),  
(4,'ha4',400),
(5,'ha5',500) 


select
    id
    ,name
    ,Mar
    ,cTime = dateadd(second,id * 20,getdate())
from @Student

或者,在插入所有内容后更新表格,如果有超过5行样本的话。

update @Student set cTime = dateadd(second,id * 20,getdate())


select * from @Student