Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Determine if Update was successful

Author  Topic 

rama108
Posting Yak Master

115 Posts

Posted - 2013-03-06 : 15:03:20
We can use @@ROWCOUNT to determine if there are any rows in SELECT statement. How do we determine if there were any records updated?

Thanks.

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-03-06 : 15:12:30
You can still use @@ROWCOUNT, but with one caveat. @@ROWCOUNT would return the number of rows touched, rather than rows where some column or other was changed. So for example, in the following, if there were any rows in the table where id = 3, you would get a non-zero number for @@ROWCOUNT even though the data was not materially changed:
UPDATE tbl SET id = 3 WHERE id = 3;
Go to Top of Page

rama108
Posting Yak Master

115 Posts

Posted - 2013-03-06 : 15:49:16
Thanks James. What if i do not have a where clause in update statement?

UPDATE TableA
FROM TableA a
JOIN TableB b
on a.ID = b.ID

Thanks.
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-03-06 : 17:02:42
It would return exactly the same number as you would get if you were to replace the update with a select
SELECT *
FROM TableA a
JOIN TableB b
on a.ID = b.ID;
SELECT @@rowcount;
Go to Top of Page
   

- Advertisement -