27,579
社区成员
发帖
与我相关
我的任务
分享
update a
set 时间列=dateadd(ss,5*id,时间列)
--> 测试数据:[TB]
if object_id('[TB]') is not null drop table [TB]
create table [TB]([D_SDateTime] datetime,[D_DateTime] sql_variant)
insert [TB]
select '2011-01-01 13:40:000',null union all
select '2011-01-02 14:20:000',null union all
select '2011-01-01 13:40:000',null union all
select '2011-01-01 13:40:000',null union all
select '2011-01-02 14:20:000',null
select * from [TB]
WITH CTE
AS(
SELECT
TOP 100 PERCENT D_SDatetime,
num = ROW_NUMBER() OVER ( PARTITION BY D_SDatetime ORDER BY GETDATE())
FROM dbo.TB
ORDER BY D_SDatetime)
SELECT D_SDatetime,D_DateTime = CASE WHEN num = 1 THEN D_SDatetime ELSE DATEADD(ss,(num-1)*5,D_SDatetime) END
FROM CTE
/*
D_SDatetime D_DateTime
----------------------- -----------------------
2011-01-01 13:40:00.000 2011-01-01 13:40:00.000
2011-01-01 13:40:00.000 2011-01-01 13:40:05.000
2011-01-01 13:40:00.000 2011-01-01 13:40:10.000
2011-01-02 14:20:00.000 2011-01-02 14:20:00.000
2011-01-02 14:20:00.000 2011-01-02 14:20:05.000
(5 行受影响)*/
;with a as (select * ,row_number()over(order by 时间列) as id from tb)
update a
set 时间列=dateadd(ss,5,时间列)
--测试库里测试!
--有主键id没?有的话:
create table tb(id int,date datetime)
insert into tb
select 1,'2011-3-2 14:00' union all
select 2,'2011-3-2 14:00' union all
select 3,'2011-3-2 14:00' union all
select 4,'2011-3-2 14:00' union all
select 6,'2011-3-2 14:00' union all
select 7,'2011-3-2 14:00' union all
select 9,'2011-3-2 14:00'
go
update tb
set date = dateadd(ss,(select count(*)*5 from tb t where id <= tb.id),date)
select *
from tb
drop table tb
/*
id date
----------- -----------------------
1 2011-03-02 14:00:05.000
2 2011-03-02 14:00:10.000
3 2011-03-02 14:00:15.000
4 2011-03-02 14:00:20.000
6 2011-03-02 14:00:25.000
7 2011-03-02 14:00:30.000
9 2011-03-02 14:00:35.000
(7 行受影响)