Selecting previous and next row using sp
- by davor
I want to select previous and next row from table based on current id. I send current id to stored procedure and use this query:
-- previous
select top 1 id from table where id < @currentId order by id desc
-- next
select top 1 id from table where id < @currentId order by id asc
The problem is when I send currentId which is the last id in the table and want to select next row. Then is nothing selected. Same problem for previous row, when I send currentId which is first id in table
Is it possible to solve this in sql without additional queries?