if i update one record in table ...after updating how can i ensure that the table has only one affected record. Im using sql server 2005
+3
A:
Use @@ROWCOUNT
USE AdventureWorks;
GO
UPDATE HumanResources.Employee
SET Title = N'Executive'
WHERE NationalIDNumber = 123456789
IF @@ROWCOUNT = 0
PRINT 'Warning: No rows were updated';
GO
Michael Valenty
2009-12-30 04:33:53
ya fine,,,,but if any one update the record in that table (same server) how can i know about this
Domnic
2009-12-30 04:36:23
@@rowcount will only tell you what happened to the table by your last statement, not from other users. Is that what you want? What exactly are you trying to do? Maybe post another question with more details.
Michael Valenty
2009-12-30 04:56:21
Would this be better as "IF @@ROWCOUNT <> 1" which would satisfy the requirement of "only one affected record"
gbn
2009-12-30 08:57:12
@gbn: Agreed, it would be better. However, if the WHERE clause of your `UPDATE` statement covers the PK, then the only possible values are 0 or 1.
Craig Young
2009-12-30 10:30:31