Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Best way to handle "Locate" type screens?

Author  Topic 

delusion_
Starting Member

1 Post

Posted - 2004-02-02 : 12:49:11
I have to build a few locate-type screens in both ASP/ASP.Net and would like to drive each locate screen from a single stored procedure. The locate screens would be potentially returning thousands of results (depending on the input parameters, obviously.) So I need some way to handle "paging" (50 results per page, etc.) -- What is the best way to make a locate stored procedure, and the best way to handle pagination?

Thanks in advance!

stephe40
Posting Yak Master

218 Posts

Posted - 2004-02-02 : 14:03:59
Not sure what you mean by "locate-type" screens.

But for paging, you can handle it on either side. ASP.NET has some built in controls to handle paging, ex. data lists. If you want to do it on the SQL Server side your going to need to use a stored procedure. Here is a SP that I use to handle paging on the sql server.



create PROCEDURE staffList @pageNum int = -1, @pageSize int, @totalHits int OUTPUT, @totalPages int OUTPUT as

DECLARE @start int ,@end int ,@err int

-- This table holds results of the query and assigns a record number
DECLARE @results TABLE (
recnum int IDENTITY(1,1),username char(8), deptName varchar(50), posName varchar(50), fname varchar(50),
lname varchar(50), email varchar(50), listed bit, access bit)

-- write the results to the table variable so that each row is assigned to a recnum
INSERT INTO @results (username, deptName, posName, fname, lname, email, listed, access)
SELECT s.username, d.deptName, p.posName, s.fname, s.lname, s.email, s.listed, s.access
FROM dbo.staff s inner join dbo.department d on s.deptID = d.deptID inner join dbo.position p on s.posID = p.posID
ORDER BY s.lname, s.fname

SELECT @err = @@ERROR, @totalHits = @@ROWCOUNT

select @totalPages = ceiling(cast(@totalHits as decimal)/ cast(@pageSize as decimal))

IF @err != 0 RETURN -1

-- calculate the beginning and ending row numbers
SET @end = @pageNum * @pageSize
SET @start = @end - @pageSize + 1

-- Return the requested page unless @pageNum = -1, in which case, return the entire result set
SELECT username, deptName, posName, fname, lname, email, listed, access
FROM @results
WHERE (@pageNum = -1 OR recnum BETWEEN @start AND @end)

IF @@ERROR != 0 RETURN -1
ELSE RETURN 0




- Eric
Go to Top of Page
   

- Advertisement -