SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Determine if Update was successful
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

rama108
Posting Yak Master

109 Posts

Posted - 03/06/2013 :  15:03:20  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

3704 Posts

Posted - 03/06/2013 :  15:12:30  Show Profile  Reply with Quote
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

109 Posts

Posted - 03/06/2013 :  15:49:16  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

3704 Posts

Posted - 03/06/2013 :  17:02:42  Show Profile  Reply with Quote
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;

Edited by - James K on 03/06/2013 17:02:59
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000