当前位置: 代码迷 >> Sql Server >> sql大侠
  详细解决方案

sql大侠

热度:39   发布时间:2016-04-27 12:59:11.0
sql在线等大侠
id timestamp
1 2012-5-4 17:07:35
2 2012-5-4 17:07:35
3 2012-5-4 17:07:35
。。。

如何更新使得下一条记录中timestamp的值是上一条中时间+1 第一条的时间不变。坐等大侠

------解决方案--------------------
SQL code
DECLARE @timestamp DATETIME UPDATE TAB SET   @timestamp = CASE WHEN @timestamp IS NULL THEN timestamp ELSE DATEADD(SECOND,1,@timestamp) END  ,timestamp = @timestamp
------解决方案--------------------
SQL code
--> 测试数据:[test]if object_id('[test]') is not null drop table [test]create table [test]([id] int,[timestamp] datetime)insert [test]select 1,'2012-5-4 17:07:35' union allselect 2,'2012-5-4 17:07:35' union allselect 3,'2012-5-4 17:07:35'update testset [timestamp]=DATEADD(DD,id-1,(select [timestamp] from test where id=1))select * from test/*id    timestamp1    2012-05-04 17:07:35.0002    2012-05-05 17:07:35.0003    2012-05-06 17:07:35.000*/
  相关解决方案