Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 update group of rows and return them from sp

Author  Topic 

tpiazza55
Posting Yak Master

162 Posts

Posted - 2012-01-04 : 18:12:12
basically i need to take a group or records flag them as in progress so nothing else uses them and return the same records.

the following works but i am sure there is a better way to do this that doesnt involve 2 queries.

any suggestions are welcomed

BEGIN TRAN

select top 100 * Orders O

UPDATE orders SET inprogress = '1'
WHERE id in
(
select top 100 * Orders O
)

IF @@ERROR <> 0 BEGIN ROLLBACK

TRAN END

COMMIT TRAN

tpiazza55
Posting Yak Master

162 Posts

Posted - 2012-01-04 : 18:37:19
for anyone else you can use output

UPDATE orders SET inprogress = '1'

output inserted.*

WHERE id in
(
select top 100 * Orders O
)

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-05 : 10:13:05
[code]
UPDATE o
SET inprogress = '1'
FROM (SELECT ROW_NUMBER() OVER (ORDER BY id) AS Rn,inprogress FROM Orders)o
WHERE Rn <=100
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -