Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Using ROW_COUNT Expand / Collapse
Author
Message
Posted Wednesday, September 12, 2012 9:51 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, September 24, 2014 9:12 AM
Points: 24, Visits: 62
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
Post #1358078
Posted Wednesday, September 12, 2012 9:59 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 7:09 PM
Points: 20,744, Visits: 32,560
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





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)
Post #1358087
Posted Thursday, September 13, 2012 2:22 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, September 24, 2014 9:12 AM
Points: 24, Visits: 62
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
Post #1358390
Posted Thursday, September 13, 2012 2:38 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Thursday, October 23, 2014 3:29 AM
Points: 1,678, Visits: 19,554

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



____________________________________________________

How to get the best help on a forum

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

Never approach a goat from the front, a horse from the rear, or a fool from any direction.
Post #1358397
Posted Thursday, September 13, 2012 2:58 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, September 24, 2014 9:12 AM
Points: 24, Visits: 62
Nope thats absolutely fine. I just wanted a way to return the total number of rows to php for pagination. Perfect thanks very much
Post #1358409
Posted Thursday, September 13, 2012 3:22 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Yesterday @ 6:05 AM
Points: 40,258, Visits: 36,681
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 2008, MVP
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

Post #1358422
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse