[英]SQL Server Stored Procedure that returns updated records
I'm trying to create a stored proc that returns all records that are updated. 我正在尝试创建一个存储过程,返回所有更新的记录。
My update query is this: 我的更新查询是这样的:
UPDATE BatchItems
SET [Status] = 'Processing'
WHERE [Status] = 'Queued'
I'm just not sure how to return those specific records. 我只是不确定如何返回这些特定记录。
You can use OUTPUT INSERTED.[cols]
to fetch rows the statement has modified. 您可以使用OUTPUT INSERTED.[cols]
来获取语句已修改的行。
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
If you're using SQL Server 2005 or above you can use the OUTPUT clause. 如果您使用的是SQL Server 2005或更高版本,则可以使用OUTPUT子句。
http://msdn.microsoft.com/en-us/library/ms177564(v=SQL.90).aspx http://msdn.microsoft.com/en-us/library/ms177564(v=SQL.90).aspx
update BatchItems
set Status = 'Processing'
output inserted.*
where Status = 'Queued'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.