sql server 2008 r2 top (1) to update date not working -


i have table 2 colums (starttime, endtime) , datatime type.

when insert row in table want update previous data in table this:

update endtime in row has recent (max) starttime 

i worked (like thread tell me) this:

begin     ;with t as(     select *, row_number() on (order starttime desc) rnum     order_status     orderid=@orderid     )     update top(1) t set endtime=@starttime     end 

but seems updates small (most old) not recent.

what right please

you're not applying ordering row_number method, want this:

begin  ;with t as(     select *, row_number() on (order starttime desc) rnum     order_status     orderid=@orderid   )   update t set endtime=@starttime   (select top 1 * t order rnum asc) t end 

or bit neater, should work (untested):

begin  ;with t as(     select top 1 *     order_status     orderid=@orderid     order starttime desc   )   update t set endtime=@starttime end 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -