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


Using ROW_COUNT


Using ROW_COUNT

Author
Message
mark.dungey 56406
mark.dungey 56406
Valued Member
Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)

Group: General Forum Members
Points: 50 Visits: 108
Hi Below is the code for my stored procedure


Alter proc [dbo].[InvoiceDisplayTest] (@InvoiceNumber nvarchar(50),@InvoiceDate datetime ,@InvoiceDate1 datetime, @AccountNumber nvarchar(50),@TradingPartnerID nvarchar(50),@Page nvarchar(50),@rownum int OUTPUT)
AS
BEGIN
SELECT * FROM (SELECT InvoiceID,DocumentNumber, convert(char(12),DocumentDate,112) AS DocumentDate,AccountNumber,Supplier,TradingPartnerID,


ROW_NUMBER()OVER(ORDER BY DocumentNumber ASC)AS rowno FROM InvoiceHeader

WHERE (@InvoiceNumber = '' OR DocumentNumber = @InvoiceNumber)
and
(@AccountNumber = '' or AccountNumber = @AccountNumber)
and
(@TradingPartnerID = '' or TradingPartnerID = @TradingPartnerID)
and
(@InvoiceDate = '' or DocumentDate >= @InvoiceDate
and
DocumentDate <=@InvoiceDate1 or @InvoiceDate1 = '')
)As qr
WHERE rowno BETWEEN ((@Page - 1) * 20 + 1)
AND (@Page * 20)

END


My Question is how do I SET my @rownum OUTPUT parameter equal to the total rows for the inner select statement. As the rows for the whole query only counts the ones being displayed. Cant i just use the alias of my ROW_NUMBER() to get the total rows. Or another way.
Looking foward to hearing from you

Mark
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39541 Visits: 38554
Give this a try:



Alter proc [dbo].[InvoiceDisplayTest] (
@InvoiceNumber nvarchar(50),
@InvoiceDate datetime ,
@InvoiceDate1 datetime,
@AccountNumber nvarchar(50),
@TradingPartnerID nvarchar(50),
@Page nvarchar(50),
@rownum int OUTPUT)
AS
BEGIN
SELECT *
FROM (SELECT InvoiceID,DocumentNumber, convert(char(12),DocumentDate,112) AS DocumentDate,AccountNumber,Supplier,TradingPartnerID,
ROW_NUMBER()OVER(ORDER BY DocumentNumber ASC)AS rowno
FROM InvoiceHeader
WHERE
(@InvoiceNumber = '' OR DocumentNumber = @InvoiceNumber)
and (@AccountNumber = '' or AccountNumber = @AccountNumber)
and (@TradingPartnerID = '' or TradingPartnerID = @TradingPartnerID)
and (@InvoiceDate = '' or DocumentDate >= @InvoiceDate
and DocumentDate <=@InvoiceDate1 or @InvoiceDate1 = '')
)As qr
WHERE
rowno BETWEEN ((@Page - 1) * 20 + 1)
AND (@Page * 20)

SET @rownum = @@rowcount;

END




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
mark.dungey 56406
mark.dungey 56406
Valued Member
Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)

Group: General Forum Members
Points: 50 Visits: 108
Hi thanks for the reply. Unfortunately that just returns the row count of the rows that i have asked for i.e always 20 as thats the limit i've set. Any other suggestions:-D
Mark Cowne
Mark Cowne
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3113 Visits: 24092
May not be what you're after, but you can return an additional column with the total row count


Alter proc [dbo].[InvoiceDisplayTest] (@InvoiceNumber nvarchar(50),@InvoiceDate datetime ,@InvoiceDate1 datetime, @AccountNumber nvarchar(50),@TradingPartnerID nvarchar(50),@Page nvarchar(50),@rownum int OUTPUT)
AS
BEGIN
SELECT * FROM (SELECT InvoiceID,DocumentNumber, convert(char(12),DocumentDate,112) AS DocumentDate,AccountNumber,Supplier,TradingPartnerID,

ROW_NUMBER()OVER(ORDER BY DocumentNumber ASC)AS rowno,

COUNT(*) OVER() AS TotalRows

FROM InvoiceHeader

WHERE (@InvoiceNumber = '' OR DocumentNumber = @InvoiceNumber)
and
(@AccountNumber = '' or AccountNumber = @AccountNumber)
and
(@TradingPartnerID = '' or TradingPartnerID = @TradingPartnerID)
and
(@InvoiceDate = '' or DocumentDate >= @InvoiceDate
and
DocumentDate <=@InvoiceDate1 or @InvoiceDate1 = '')
)As qr
WHERE rowno BETWEEN ((@Page - 1) * 20 + 1)
AND (@Page * 20)

END



____________________________________________________

Deja View - The strange feeling that somewhere, sometime you've optimised this query before

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537




mark.dungey 56406
mark.dungey 56406
Valued Member
Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)

Group: General Forum Members
Points: 50 Visits: 108
Nope thats absolutely fine. I just wanted a way to return the total number of rows to php for pagination. Perfect thanks very much
GilaMonster
GilaMonster
SSC Guru
SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)

Group: General Forum Members
Points: 87659 Visits: 45272
You might also want to have a read through this: http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


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