Wednesday 11 September 2013

Sql server - dealing with new data?

Sql server - dealing with new data?

A rock band has currently 100 songs :
select count(songName) from bands where name='Beatles'
result : 100.
I display those songs in my app via paging ( each time - 10 results)
How do I get the relevant page and its rows ?
like this : (SP)
declare @pageItems int=10
declare @pagenum int=1
select * from (SELECT [id] , row_number() over ( order by songName) as n
FROM Bands where name='Beatles') a
where a.n > (@pagenum-1)*@pageItems and a.n<= (@pagenum)*@pageItems
But there is a problem.
Suppose a user is at page 3.
And the beatles publish a new song named : "aaa"
So now , there will be a mismatch because there is a new row which is
inserted at the top ( and pushes all rows below).
I dont want to get all the rows into my app.
What is the correct way / (feature?) to get the first criteria results ?
I could use a temp table but it will be only for the current connection. (
and each time a user press "next" at paging - it's a new session).

No comments:

Post a Comment