How can I get the number of rows affected by an UPDATE query in a Stored Procedure (SQL Server 2005), as a resultset. e.g.
CREATE PROCEDURE UpdateTables AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; UPDATE Table1 Set Column = 0 WHERE Column IS NULL UPDATE Table2 Set Column = 0 WHERE Column IS NULL UPDATE Table3 Set Column = 0 WHERE Column IS NULL UPDATE Table4 Set Column = 0 WHERE Column IS NULL END
Then return:
Table1 Table2 Table3 Table4 32 45 0 3
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.
@@ROWCOUNT returns the affected rows from any statement, even if it's not DML or a SELECT query.
For Microsoft SQL Server you can return the @@ROWCOUNT variable to return the number of rows affected by the last statement in the stored procedure.
Get the Number of Rows Affected Using the execute() Method The execute() method executes the given SQL query, which may return multiple results.
CREATE PROCEDURE UpdateTables AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; DECLARE @RowCount1 INTEGER DECLARE @RowCount2 INTEGER DECLARE @RowCount3 INTEGER DECLARE @RowCount4 INTEGER UPDATE Table1 Set Column = 0 WHERE Column IS NULL SELECT @RowCount1 = @@ROWCOUNT UPDATE Table2 Set Column = 0 WHERE Column IS NULL SELECT @RowCount2 = @@ROWCOUNT UPDATE Table3 Set Column = 0 WHERE Column IS NULL SELECT @RowCount3 = @@ROWCOUNT UPDATE Table4 Set Column = 0 WHERE Column IS NULL SELECT @RowCount4 = @@ROWCOUNT SELECT @RowCount1 AS Table1, @RowCount2 AS Table2, @RowCount3 AS Table3, @RowCount4 AS Table4 END
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With