1

I know that GO is used in SQL to terminate a batch and to control things like

ALTER TABLE tableName ADD name VARCHAR(20)
GO -- this GO will make sure that "name" will be added before the select statement 
SELECT name FROM tableName

But can GO also be used to prevent delay problems? An example

DECLARE @countID int
SELECT @countID = COUNT(id) FROM Usuer -- this table has milions of registers and takes a while to be executed

--GO

SELECT @countID, Name FROM Something

SQL Server already wait for the first select to finish and then start the second select, or should I put the GO between the two statements to guarantee that the second select will be executed only after the first one?

thanks

3
  • 5
    SQL Server will execute the statements in order, regardless of whether or not there is a GO between them. Commented Oct 22, 2014 at 13:46
  • "GO is used in SQL" is not exactly true. GO is used in SQL Server Management Studio to terminate a batch. It is not part of the SQL language (neither standard SQL nor T-SQL). Commented Oct 22, 2014 at 13:54
  • Side note, if you truly wanted a delay, you would use WaitFor, but one query window will always run in sequence, waiting for the previous command to complete. If you need parallel running (which I don't think you do), you can look at parallel-execution-of-stored-procedures-in-job-sql-server for an example Commented Oct 22, 2014 at 13:59

1 Answer 1

1

GO is a statement of Management Studio, not SQL Server. It will not be sent to the server at all.

Instead, the whole queries between the GOs will be sent as separate commands, one after another. The reason why you had to use GO between some statements was because the query was analyzed and planned in a way that couldn't handle structure modifying queries - it couldn't build the plan, because your table didn't have the column yet! This is no longer the case on MS SQL.

It doesn't do any delays or anything, other than the fact that it requires more communication with the server.

Sign up to request clarification or add additional context in comments.

2 Comments

Also, if there is an error in one of the statements "GO" will ensure other statements are executed. Correct me if I am wrong.
@ankk Indeed. Which can bite you badly, of course. There's a few other differences as well, all related to the fact that the commands are executed as separate commands.

Your Answer

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.