0

I have a requirement to return a batch of records and then mark the batch complete. I was thinking to acheive this with a select and the update. Something like

create procedure testproc
as
select x,y,z from Table
update Table set retreived = 'Y'
go 

The question I have, is what happens if the procedure executes, returns the select but the connection breaks before the batch is retreived. Does the batch still get updated?

Otherwise can anyone think of a better way to acheive this with stored procs?

asked Oct 4, 2012 at 14:56

1 Answer 1

2

You can do this in a single statement using the OUTPUT clause:

UPDATE dbo.Table
SET Retrieved = 'Y'
OUTPUT inserted.x, inserted.y, inserted.z;

If you don't really mean to update the entire table every time:

UPDATE dbo.Table
SET Retrieved = 'Y'
OUTPUT inserted.x, inserted.y, inserted.z
WHERE <...where clause...>;
answered Oct 4, 2012 at 15:02
4
  • Yes, its more efficient. My question is really "is the Sql protocol intelligent enough to notice that connection has dropped before it completes the update?" Commented Oct 4, 2012 at 15:14
  • It's not about efficiency, it's about being atomic. You can't get the results of the select unless the update has succeeded. This isn't true when you separate the actions into two statements. SQL Server will not send some kind of notification to your app that the select results are not valid - which wouldn't be possible anyway if the connection is severed. It also won't (and can't) roll back a select because the update failed (or never started). Commented Oct 4, 2012 at 15:41
  • @Aaron: maybe you should add that as a request to connect? "Allow us to Rollback SELECT statements?" ;-) Commented Oct 4, 2012 at 20:46
  • Thanks for the response Aaron, what do you think would be the behaviour when the update succeeded but the connection got severed? would the update complete or would it rollback. Commented Oct 8, 2012 at 10:15

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.