cilerler cilerler - 7 months ago 12
SQL Question

Retrieving affected records through T-SQL

Following sample returns affected ROWCOUNT. Is there a way to return affected IDENTITYs?

UPDATE HumanResources.Employee
SET JobTitle = N'Executive'
WHERE NationalIDNumber = 123456789
IF @@ROWCOUNT = 0
PRINT 'Warning: No rows were updated';

Answer
UPDATE HumanResources.Employee 
SET JobTitle = N'Executive'
OUTPUT INSERTED.PrimaryKeyID
WHERE NationalIDNumber = 123456789

Here I assumed Id name is PrimaryKeyID.