@@ROWCOUNT output, the given choice for standard out logging

I still don't know what is causing SSMS to output rowcount differently from QA, but to be safe one should use @@ROWCOUNT to output counts for dynamic SQL statements.

SET NOCOUNT ON
DECLARE @vcSQL NVARCHAR(4000)
SET @vcSQL = 'SELECT * FROM pubs..authors;'
PRINT @vcSQL
SET NOCOUNT OFF
PRINT 'Test1: sp_executesql'
EXEC sp_executesql @vcSQL
PRINT 'Test2: EXEC ()'
EXEC (
@vcSQL
)
PRINT '(There were ' + cast(@@ROWCOUNT as varchar) + ' row(s) affected)'
/*
----------------------------------------
Test1 **SSMS output
SELECT * FROM pubs..authors;
Test1: sp_executesql
(23 row(s) affected)
Test2: EXEC ()
(23 row(s) affected)
(There were 23 row(s) affected)
----------------------------------------
Test2 **QueryAnalyzer output
SELECT * FROM pubs..authors;
Test1: sp_executesql
(23 row(s) affected)
Test2: EXEC ()
(There were 23 row(s) affected)
----------------------------------------
*/

Comments are closed.

Post Navigation