开发者

Update & Return from Stored Procedure withouth Repeating Query

开发者 https://www.devze.com 2023-03-13 15:22 出处:网络
Having defined a view in SQL Server 2005, I want to write a stored procedures which returns the top n elements from this view according to some sorting criteria. However, before returning them I must

Having defined a view in SQL Server 2005, I want to write a stored procedures which returns the top n elements from this view according to some sorting criteria. However, before returning them I must do some updates to these results, but I can't figure out how to do it without querying the view twice... My solution would be

CREATE PROCEDURE UpdateAndReturn

AS

   开发者_如何学JAVA UPDATE TableToUpdate SET Field = @Something WHERE IDRef IN (SELECT TOP (n) ID FROM View ORDER BY This, That)

    SELECT TOP (n) * FROM View ORDER BY This, That

GO

Can anyone help me finding anything more elegant and efficient than this?


Query the view first and put the result to a table variable or a temporary table. Use that table in your update statement and finally do select * from the temp table.

0

精彩评论

暂无评论...
验证码 换一张
取 消