user2571510
user2571510

Reputation: 11377

SQL Server: add total count to select

I am using the following stored procedure to fetch some basic data from a database which works fine so far.

Can someone tell me how I can add a total count here without creating an extra column ? What I want is that in addition to fetching my data I just want to know how many records are returned which also needs to cover the case that there are no matching records.

My stored procedure:

ALTER PROCEDURE [dbo].[CountQueue]
AS
BEGIN
    SET NOCOUNT ON;
    SELECT      dateEsc,
                url,
                EID
    FROM        QueueLog
    WHERE       logStatus = 'New'
    AND         region = 'US'
    AND         (
                    flag = 'flag1' 
                    OR 
                    flag = 'flag2'
                )
    ORDER BY    dateEsc desc, EID desc
END

Many thanks for any help with this, Tim.

Upvotes: 0

Views: 222

Answers (1)

Ziouas
Ziouas

Reputation: 519

You can use enviromental variable @@ROWCOUNT. It stores the number of rows affected by last executed statement.

Here is the reference: http://technet.microsoft.com/en-us/library/ms187316.aspx

Upvotes: 1

Related Questions