Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Pagination in stored procedure


Pagination in stored procedure

Author
Message
Chris Smith-266894
Chris Smith-266894
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 7
Comments posted to this topic are about the item Pagination in stored procedure
AnzioBake
AnzioBake
SSC Veteran
SSC Veteran (212 reputation)SSC Veteran (212 reputation)SSC Veteran (212 reputation)SSC Veteran (212 reputation)SSC Veteran (212 reputation)SSC Veteran (212 reputation)SSC Veteran (212 reputation)SSC Veteran (212 reputation)

Group: General Forum Members
Points: 212 Visits: 672
Unless I am misunderstanding this procedure is incorrect.

My understanding is that the
@PageIndex indicates which page number is required
and @PageSize is the number of records per Page

Exec SP @PageIndex = 1, @PageSize = 10 results in rows 1-10
Exec SP @PageIndex = 2, @PageSize = 10 results in rows 11-20

Your Procedure is returning the following
Exec SP @PageIndex = 1, @PageSize = 10 results in rows 1-10
Exec SP @PageIndex = 2, @PageSize = 10 results in rows 2-11


I believe you want something like

CREATE PROCEDURE [dbo].[uspOrderReport] (@PageIndex INT, @PageSize INT) AS

DECLARE @Rows INT
DECLARE @tempIDs TABLE
(
[identity] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY CLUSTERED,
[OrderID] [int] NULL
) --TO STORE THE SCORED TABLE

SELECT @Rows = ( @PageIndex * @PageSize )
SET ROWCOUNT @Rows

INSERT INTO
@tempIDs (OrderID)
SELECT
OrderId
FROM
orders
ORDER BY OrderID

SELECT
ORD.OrderId,
ORD.CustomerId,
ORD.EmployeeId
,[tid].[Identity]
FROM
orders ORD LEFT OUTER JOIN @tempIDs TID
ON ORD.OrderID = TID.OrderID
WHERE
tid.[Identity] > ( @PageIndex - 1 ) * @PageSize
ORDER BY ORD.OrderID

RETURN


go


exec [dbo].[uspOrderReport] 1, 20
exec [dbo].[uspOrderReport] 1, 10
exec [dbo].[uspOrderReport] 2, 10



Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search