tags:

views:

35

answers:

1

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
ya fine,,,,but if any one update the record in that table (same server) how can i know about this
Domnic
@@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
Would this be better as "IF @@ROWCOUNT <> 1" which would satisfy the requirement of "only one affected record"
gbn
@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