I am running some update statements in SQL and I want to return to an Audit table how many rows have been affected. Now I understand (and have seen examples) that a @@ROWCOUNT can be used to do this. The problem is I'm using an update statement with an IF statement.
BEGIN TRANSACTION
WHILE 1=1
BEGIN
BEGIN TRANSACTION
--update a 1000 rows at a time
UPDATE TOP (1000) table1
SET flag = 1,
WHERE ID IN (SELECT ID FROM #list)
IF @@ROWCOUNT = 0 -- terminating condition
BEGIN
COMMIT TRANSACTION
BREAK
END
COMMIT TRANSACTION
WAITFOR DELAY '00:00:01';
END
--COMMIT TRANSACTION
ROLLBACK TRANSACTION
GO
Now I want to use the something like the following.
INSERT INTO @Audit
SELECT 'table1', @@ROWCOUNT
But no matter where I put it in the update it always returns 0. I have tried reassigning it to a different variable but still no joy.
Any pointers on this would be great.
If more information is needed please ask :)