Right now I have this code to find next and previous rows using SQL Server 2005. intID is the gallery id number using bigint data type:
SQL = "SELECT TOP 1 max(p.galleryID) as previousrec, min(n.galleryID) AS nextrec FROM gallery AS p CROSS JOIN gallery AS n where p.galleryid < '"&intID&"' and n.galleryid > '"&intID&"'"
Set rsRec = Server.CreateObject("ADODB.Recordset")
rsRec.Open sql, Conn
strNext = rsRec("nextrec")
strPrevious = rsRec("previousrec")
rsRec.close
set rsRec = nothing
Problem Number 1:
The newest row will return nulls on the ‘next record’ because there is none. The oldest row will return nulls because there isn’t a ‘previous record’. So if either the ‘next record’ or ‘previous record’ doesn’t exist then it returns nulls for both.
Problem Number 2:
I want to create a stored procedure to call from the DB so intid can just be passed to it
TIA
This will yield
NULLfor previous on the first row, andNULLfor next on the last row. Though your ordering seems backwards to me; why is “next” lower than “previous”?Also, it doesn’t make sense to want an empty string instead of
NULL. Your ASP code can deal withNULLvalues just fine, otherwise you’d have to convert the resulting integers to strings every time. If you really want this you can say:But this will no longer work well when you move from ASP to ASP.NET because types are much more explicit. Much better to just have the application code be able to deal with, instead of being afraid of,
NULLvalues.This seems like a lot of work to get the previous and next ID, without retrieving any information about the current ID. Are you implementing paging? If so I highly recommend reviewing this article and this follow-up conversation.