SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to set out paramater when paging with t-sql and ROW_NUMBER()


How to set out paramater when paging with t-sql and ROW_NUMBER()

Author
Message
BuilderBits
BuilderBits
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1335 Visits: 251
Is there a way that I can set the output parameter when I've got my t-sql written the way that I do, and if now whats the recommendation?

The ,@Total = COUNT(*) in the follow code does not work. How can I set the output parameter for paging?



USE [GenericCatalog]
GO
/****** Object: StoredProcedure [Generic].[proc_GetPartsForUserByCategory] Script Date: 8/27/2013 12:19:10 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [Generic].[proc_GetPartsForUserByCategory]
@UserID UNIQUEIDENTIFIER,
@GenericCatalogID INT,
@CategoryID INT,
@StartIndex INT,
@PageSize INT,
@Total INT OUT
AS

SET NOCOUNT ON;
SET @StartIndex = @StartIndex + 1

BEGIN
SELECT *
FROM (
SELECT *, ROW_NUMBER() OVER (ORDER BY ID ASC) AS RowNum, COUNT(*) OVER() AS Total
FROM (
SELECT p.*,gc.id'GenericCatalogID',@Total = COUNT(*),
gc.SupplierName,gc.SupplierEmail,gc.SupplierPhone,
pr.[Profile],pr.Siteline,pr.Depth

FROM [Generic].[Part] p WITH(NOLOCK) JOIN
Generic.UserPart up WITH(NOLOCK) ON up.PartID = p.ID JOIN
Generic.GenericCatalog gc WITH(NOLOCK) ON gc.ID = up.GenericCatID JOIN
Generic.[ProfileS] pr WITH(NOLOCK) ON p.ProfileID = pr.ID

WHERE p.ID = up.PartID
AND
CategoryID = @CategoryID
AND
gc.UserID = @UserID
AND gc.ID = @GenericCatalogID

) AS firstt
) AS final
WHERE RowNum BETWEEN @StartIndex AND (@StartIndex + @pageSize) - 1

ORDER BY final.Number ASC;


END;
SET NOCOUNT OFF;



Dam again!
TheSQLGuru
TheSQLGuru
SSC-Dedicated
SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)

Group: General Forum Members
Points: 31771 Visits: 8671
To my knowledge as long as you try to do something like this:

USE AdventureWorks2012
GO
DECLARE @totalrows int

SELECT *, @totalrows = TotalRows
FROM (
SELECT JobTitle,
BusinessEntityID,
ROW_NUMBER() OVER (ORDER BY BusinessEntityID) as RowNum,
COUNT(*) OVER () as TotalRows ---- nice to know how many total rows when paging to know how many pages are there
FROM HumanResources.Employee) AS t
WHERE RowNum BETWEEN 5 AND 15



you are going to get an error like this:

Msg 141, Level 15, State 1, Line 3
A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations.



I can't think of a way off the top of my head to get the total EXPECTED rows while also FILTERING to a restricted set for output in a single pass. You store the data and use @@ROWCOUNT and then SELECT final set out (possibly using just key values to make a smaller temp objects - I have used that to GREAT effect in paging scenarios at clients over the years) or you do one hit as a count to set the output parameter and a second to get the data. With good indexing it can still be "fast", but clearly not as fast as if you could get the total with the restricted set...

Best,
Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru on googles mail service
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