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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Outputting number of Rows Updated

Status
Not open for further replies.

johnpienaar

Technical User
Jun 23, 2004
18
0
0
ZA
Hi
I was wondering if anyone knew a way of updating a field with the number of rows that were updated by a specified query.
For example if I run an update query in query analyzer and it returns "2000 rows affected by query"- is there anyway for me to update another table with that value (2000)?
Help would be much appreciated.
John
 
Something like this:

Code:
DECLARE @row_count int

--do your first update here
UPDATE table1...

--get the number of rows affected
SET @row_count = @@ROWCOUNT

--insert rowcount into your table
INSERT table2 VALUES (@row_count)

--James
 
Within a stored procedure you may obtain the number of rows affected by the last statement in a variable named @@ROWCOUNT.

Code:
DECLARE @nRowsAffected INT
. . .
UPDATE MyTable SET work_completed = 1 WHERE answer IS NOT NULL
SET @nRowsAffected = @@ROWCOUNT

UPDATE MyTrackingTable SET last_update_count = @nRowsAffected

 
Thanks for the quick replies- I'll test these out now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top