Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

select x,y,z from Table

update Table set retreived = 'Y'


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?

share|improve this question

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...>;
share|improve this answer
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?" – blakmk Oct 4 '12 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). – Aaron Bertrand Oct 4 '12 at 15:41
@Aaron: maybe you should add that as a request to connect? "Allow us to Rollback SELECT statements?" ;-) – RBarryYoung Oct 4 '12 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. – blakmk Oct 8 '12 at 10:15

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.