UnDiUdin
UnDiUdin

Reputation: 15394

How to force sql server to execute one command at a time without using GO?

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.

Upvotes: 3

Views: 8501

Answers (2)

A-K
A-K

Reputation: 17090

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.

Upvotes: 3

Andrew Barber
Andrew Barber

Reputation: 40160

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

Upvotes: 3

Related Questions