sql2005
有一表 tableA
id flag oldtime newtime
11 0 2014-10-31 10:21:03
12 1 2014-10-31 10:21:03
11 2 2014-10-31 10:21:03
11 0 2014-10-31 10:21:03
11 0 2014-10-31 10:21:03
11 0 2014-11-01 15:25:03
假设我现在知道某个id值,要更新该id对应的flag状态为2, 判断条件是newtime时间与oldtime的时间间隔是30s之内
其中,newtime的值是 getDate()系统时间
这个表里有50多万条数据,且每日还在增加数据,这表里没主键, 所以有没有高效的更新数据写法?
------解决思路----------------------
update tableA set flag=2 where ID=@ID and [newtime]>=dateadd(s,-30,getDate())
在ID和newtime建上索引
------解决思路----------------------
改一下栏位就行了,@newtime=getdate(),为何不直接用
update tableA set flag=2 where ID=@ID and [oldtime]>=dateadd(s,-30,@newtime)
------解决思路----------------------
楼上正解~~~,不过后面的语句写错了列名[oldtime],之前是写的对的。