views:

43

answers:

2

I would like to make sure that a series of commands are executed in a serial way, like in

update TABLE1...
update TABLE2...
update TABLE3...

I would like that update TABLE2 starts only when update TABLE1 has completed.

Of course I can do this with GO:

update TABLE1...
GO
update TABLE2...
GO
update TABLE3...
GO

But in this case i will lose all the local variables.

Is there an easy way to perform what I need? Thanks.

+3  A: 

You don't need GO to do that; GO simply causes a batch to be sent to the server at that point. As long as you put the commands sequentially in one script, they will execute one-after-another just as you wish. You would have to do extra work to get them to run in parallel

Andrew Barber
This is not what I experience: for example I made a script that dropped 50 indexes, then used a cursor to do something on tables, than I recreated indexes again and I noticed that I got "cannot recreate index, it already exists" errors, because basically for some reason the commands where not executed sequentially. THat time I solved the problem by putting GO after the DROP indexes, Go after cursor operations. This is why I ask.
See AlexKuznetsov's answer for more about this. The commands ARE being run sequentially; they just can not be compiled because you can not submit a command to create an index that already exists. (even if that command batch is previously dropping that index.)
Andrew Barber
+2  A: 

Next command in your batch will begin only after the previous one has finished. No need to do anything, this is how it works. What is the problem you are solving?

Edit: your "cannot recreate index, it already exists" error occurs at compilation, not during running - your index exists when your batch is being compiled. GO breaks your script into separate batches, which compile only after the previous batch completed.

AlexKuznetsov
Please see my comment to Andrew Barber answer.