|
fizgig
Starting Member
34 Posts |
Posted - 2004-04-08 : 09:35:43
|
Hi,I use a great stored procedure for paging (see below). I use it a lot, but i have to customize it for every table or view that should be paged. I want to edit it so i only have one universal stored procedure for paging that can be called like this: sp_pager table_name, order_by_field, page_number, recs_per_page. That will save me a lot of timeDoes anyone has a good suggestion where to start? Bye,Ward###CREATE PROCEDURE paged ( @Newsletter varchar(200), @RecsPerPage int, @Page int, @orderColumn varchar (250), @direction varchar (250) )AS-- We don't want to return the # of rows inserted-- into our temporary table, so turn NOCOUNT ONSET NOCOUNT ON--Create a temporary tableCREATE TABLE ##TempPagedSubscriber( [id] [numeric](18, 0) IDENTITY (1, 1) NOT NULL , [realid] [numeric](18, 0) NULL, [name] [varchar](250) NULL, [email] [varchar](250) NULL, [subscribed] [bit] NULL, [since] [datetime] NULL, [organisation][varchar] (100) NULL, [department_function][varchar] (100) NULL, [telephone][varchar] (20) NULL, )-- Insert the rows from tblItems into the temp. tableExec ('INSERT INTO ##TempPagedSubscriber (realid, name, email, subscribed, since, organisation,department_function,telephone) SELECT id, name, email, subscribed, since, organisation, department_function, telephone FROM subscriber WHERE newsletter = ' + @Newsletter + ' ORDER BY ' + @orderColumn + ' ' + @direction) -- Find out the first and last record we wantDECLARE @FirstRec int, @LastRec intSELECT @FirstRec = (@Page - 1) * @RecsPerPageSELECT @LastRec = (@Page * @RecsPerPage + 1)-- Now, return the set of paged records, plus, an indiciation of we-- have more records or not!SELECT *, MoreRecords = ( SELECT COUNT(*) FROM ##TempPagedSubscriber TI WHERE TI.ID >= @LastRec ) FROM ##TempPagedSubscriberWHERE ID > @FirstRec AND ID < @LastRec DROP TABLE ##TempPagedSubscriber-- Turn NOCOUNT back OFFSET NOCOUNT OFFGO |
|