Skip to main content

SQL Interaction and @@ROWCOUNT

  • April 21, 2016
  • 4 replies
  • 1 view

Is there any way when processing a SQL statement (SELECT, INSERT, UPDATE, DELETE) to retrieve the results of @@ROWCOUNT to establish how many rows were affected by the statement?


#SQL
#COBOLSQLCOBSQL
#COBOL

4 replies

Lanter Werner
  • Participating Frequently
  • April 21, 2016

Is there any way when processing a SQL statement (SELECT, INSERT, UPDATE, DELETE) to retrieve the results of @@ROWCOUNT to establish how many rows were affected by the statement?


#SQL
#COBOLSQLCOBSQL
#COBOL

The following code works for me:

          exec sql delete Identity_Nummer

                    where Nummer = 0

          end-exec

          declare  rowCount as binary-long.

          exec sql select @@rowcount

                     into :rowCount

          end-exec.


Is there any way when processing a SQL statement (SELECT, INSERT, UPDATE, DELETE) to retrieve the results of @@ROWCOUNT to establish how many rows were affected by the statement?


#SQL
#COBOLSQLCOBSQL
#COBOL

Thanks for your response.

Your code does work however in an OLTP type environment there is a reasonable chance that @@ROWCOUNT will have been reset by another interaction that has occurred between the time of the deletion and you submitting the query to retrieve it.


Lanter Werner
  • Participating Frequently
  • April 21, 2016

Is there any way when processing a SQL statement (SELECT, INSERT, UPDATE, DELETE) to retrieve the results of @@ROWCOUNT to establish how many rows were affected by the statement?


#SQL
#COBOLSQLCOBSQL
#COBOL

I believe that SQL Server maintains a separate @@ROWCOUNT for each Connection/transaction. I checked this with 2 SQL queries.


Is there any way when processing a SQL statement (SELECT, INSERT, UPDATE, DELETE) to retrieve the results of @@ROWCOUNT to establish how many rows were affected by the statement?


#SQL
#COBOLSQLCOBSQL
#COBOL

Thanks for your response I will give this a try and see whether it all works.