通过T-SQL检索受影响的记录

时间:2016-05-06 13:24:18

标签: sql sql-server tsql

以下示例返回受影响的ROWCOUNT。有没有办法返回受影响的IDENTITY?

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

3 个答案:

答案 0 :(得分:3)

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

OUTPUT

答案 1 :(得分:2)

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

这里我假设Id名称是PrimaryKeyID。

答案 2 :(得分:0)

从这里https://msdn.microsoft.com/ru-ru/library/ms177564(v=sql.120).aspx

DELETE FROM dbo.table1
OUTPUT DELETED.* INTO @MyTableVar
WHERE id = 4 OR id = 2;