sql - Will TOP always return the same rows -


i have written code move data archive table, 1000 rows @ time (sql server 2008 r2):

-- archive data while (@quitloop = 0) begin    begin transaction    insert mytransactiontable (...)    select top 1000 * mytransactiontable dateofsale < @archivecutoffdate           delete top 1000 mytransactiontable dateofsale < @archivecutoffdate             if (@@rowcount = 0) select @quitloop = 1  commit transaction end  

will top returns same 1000 rows?

so rows being inserted archive table same rows being deleted form transaction table.

as using them no, not guaranteed return same rows.

why? because don't specify order by clause. it'll whatever 1000 records engine decides 'top' ones time around.

also note it's possible if specify order by still wouldn't same 1000 rows... if order not selective enough on border of 1000th item specific, definable order.

for example; if there 1002 items same sorted value @ top, don't know 2 not included.


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -