Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

@@Rowcount always returns 1

Status
Not open for further replies.

Naoise

Programmer
Dec 23, 2004
318
IE
I have a table tblA with 8 rows

tblA
-----
a | b

when I run :
exec sp_RunSQL 'update tblA set a = 8', 1, 0, ''

I always see @@ROWCOUNT printing out 1, yet query analyser will say 8 rows affected. Any ideas?

CREATE PROCEDURE sp_RunSQL(
@SQL varchar(3000)
,@SQLType int
,@RowsAffected int output
,@SQLMessage varchar(1000) output
)
AS
BEGIN

-- SQLType 1:SELECT,2:INSERT,3:UPDATE,4:DELETE

EXEC (@SQL)

IF @@ERROR <> 0
BEGIN
SET @SQLMessage = 'Failed to run SQL'
RETURN 1
END
ELSE
BEGIN
SET @RowsAffected = @@ROWCOUNT
SET @SQLMessage = 'Successfully ran SQL'
PRINT @@ROWCOUNT
RETURN 0
END

END

GO
 
Code:
....
DECLARE @lnRowCount int

SET @SQL = 'SELECT .............; SET @MyOut = @@ROWCOUNT'
sp_executesql @SQL, '@MyOut int OUTPUT', @MyOut=@lnRowCount OUTPUT

not tested

Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
 

The answer has to do with the fact that SET @foo = @@ROWCOUNT has to be placed immediately after the EXEC(@SQL) statement otherwise it doesn't work
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top