SQL Server stored procedure returning updated records

I am trying to create a saved process that returns all updated records.

My update request:

UPDATE BatchItems SET [Status] = 'Processing' WHERE [Status] = 'Queued' 

I just don’t know how to return these specific entries.

+4
source share
2 answers

You can use OUTPUT INSERTED.[cols] to select the rows that the statement changed.

 UPDATE BatchItems SET [Status] = 'Processing' OUTPUT INSERTED.* WHERE [Status] = 'Queued' 

Example;

 select 'Queued ' as status, 1 as id into #BatchItems insert #BatchItems values ('Queued', 2) insert #BatchItems values ('XXX', 3) UPDATE #BatchItems SET [Status] = 'Processing' OUTPUT INSERTED.* WHERE [Status] = 'Queued' >>status id >>Processing 2 >>Processing 1 
+4
source

If you are using SQL Server 2005 or higher, you can use the OUTPUT clause.

http://msdn.microsoft.com/en-us/library/ms177564(v=SQL.90).aspx

 update BatchItems set Status = 'Processing' output inserted.* where Status = 'Queued' 
+4
source

All Articles