Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting no. of rows affected after running select query in SQL Server 2005

Below is my query

select    
@monNameStr as [MName],             
IsNull(count(c.AssignmentID),0),                
IsNull(sum(s.ACV),0),               
IsNull(sum(s.GrossReturn),0),               
IsNull(sum(s.NetReturn),0),             
IsNull(avg(a.Total),0)          
FROM

dbo.Assignment_ClaimInfo c,             
dbo.Assignment_SettlementInfo s,                
dbo.Assignment_AdvCharges a         

Where
c.Assignmentid=s.Assignmentid and               
s.Assignmentid=a.Assignmentid and               
a.Assignmentid in                   

(select AssignmentID from dbo.Assignment_ClaimInfo                  
where (upper(InsuranceComp)=upper(@CompName) or upper(@CompName)='ALL COMPANIES') 
and (DateName(month,DATEADD(month, 0, DOFileClosed))+' '
+cast(year(DATEADD(month, 0, DOFileClosed)) as varchar)=@monNameStr))
Group By c.InsuranceComp
Order By c.InsuranceComp

where @monNameStr is calculated date field like 'October 2009'

What i need to know the no. of records affected by this select query.

I DONT NEED TO NEST THIS QUERY TO ANOTHER QUERY WITH COUNT() FUNCTION.

Your valuable help is appreciated.

like image 497
IrfanRaza Avatar asked Oct 09 '09 13:10

IrfanRaza


People also ask

How do you get the number of rows affected by a query?

MySQL ROW_COUNT() can be used to get the total number of rows affected by MySQL query. To illustrate it we are creating a procedure with the help of which we can insert records in a table and it will show us how many rows have been affected.

How can you know the number of rows affected by last SQL statement?

Usage. SQL Server @@ROWCOUNT is a system variable that is used to return the number of rows that are affected by the last executed statement in the batch.

How do I return the number of rows affected by a stored procedure?

DBCOUNT returns the number of rows affected by the last select statement executed by the stored procedure. For example, if the last two statements executed by the procedure are select and update statements, DBCOUNT returns the number of rows affected by the select, not by the update.

Which method returns the number of rows affected by the execution of a query?

Get the Number of Rows Affected Using the execute() Method The execute() method returns true if the first result is a ResultSet object; it returns false if it is an update count or there are no results.


2 Answers

capture @@ROWCOUNT into a variable, because it will change values each time you select it:

DECLARE @Rows   int

---your query here

SELECT @Rows=@@ROWCOUNT

you can then use it as necessary as @Rows

like image 67
KM. Avatar answered Oct 31 '22 14:10

KM.


You can check the value of @@ROWCOUNT after the query has run. See http://technet.microsoft.com/en-us/library/ms187316.aspx for more info.

like image 23
TLiebe Avatar answered Oct 31 '22 12:10

TLiebe