Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get number of rows inserted by a transaction

I have to manage a log where i have to see the number of rows that are inserted by a transaction. Is there any way of doing it dynamically ?

like image 242
HotTester Avatar asked Feb 16 '10 12:02

HotTester


People also ask

How do I find the number of rows inserted in SQL?

The SQL COUNT( ) function is used to return the number of rows in a table. It is used with the Select( ) statement.

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.

What is @@ rowcount?

Data manipulation language (DML) statements set the @@ROWCOUNT value to the number of rows affected by the query and return that value to the client. The DML statements may not send any rows to the client.

How do you show the number of rows?

Just click the column header. The status bar, in the lower-right corner of your Excel window, will tell you the row count. Do the same thing to count columns, but this time click the row selector at the left end of the row.


2 Answers

@@ROWCOUNT will give the number of rows affected by the last SQL statement, it is best to capture it into a local variable following the command in question, as its value will change the next time you look at it:

DECLARE @Rows int DECLARE @TestTable table (col1 int, col2 int) INSERT INTO @TestTable (col1, col2) select 1,2 union select 3,4 SELECT @Rows=@@ROWCOUNT SELECT @Rows AS Rows,@@ROWCOUNT AS [ROWCOUNT] 

OUTPUT:

(2 row(s) affected) Rows        ROWCOUNT ----------- ----------- 2           1  (1 row(s) affected) 

you get Rows value of 2, the number of inserted rows, but ROWCOUNT is 1 because the SELECT @Rows=@@ROWCOUNT command affected 1 row

if you have multiple INSERTs or UPDATEs, etc. in your transaction, you need to determine how you would like to "count" what is going on. You could have a separate total for each table, a single grand total value, or something completely different. You'll need to DECLARE a variable for each total you want to track and add to it following each operation that applies to it:

--note there is no error handling here, as this is a simple example DECLARE @AppleTotal  int DECLARE @PeachTotal  int  SELECT @AppleTotal=0,@PeachTotal=0  BEGIN TRANSACTION  INSERT INTO Apple (col1, col2) Select col1,col2 from xyz where ... SET @AppleTotal=@AppleTotal+@@ROWCOUNT  INSERT INTO Apple (col1, col2) Select col1,col2 from abc where ... SET @AppleTotal=@AppleTotal+@@ROWCOUNT  INSERT INTO Peach (col1, col2) Select col1,col2 from xyz where ... SET @PeachTotal=@PeachTotal+@@ROWCOUNT  INSERT INTO Peach (col1, col2) Select col1,col2 from abc where ... SET @PeachTotal=@PeachTotal+@@ROWCOUNT  COMMIT  SELECT @AppleTotal AS AppleTotal, @PeachTotal AS PeachTotal 
like image 170
KM. Avatar answered Oct 02 '22 19:10

KM.


In case you need further info for your log/audit you can OUTPUT clause: This way, not only you keep the number of rows affected, but also what records.

As an example of the Output Clause during inserts: SQL Server list of insert identities

DECLARE @InsertedIDs table(ID int);  INSERT INTO YourTable     OUTPUT INSERTED.ID         INTO @InsertedIDs      SELECT ... 

HTH

like image 40
ips1 38 Avatar answered Oct 02 '22 21:10

ips1 38