Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How SQL statements execute in SQL Server Management Studio with GO and without GO statement?

I have a simple query

CREATE TABLE #tempTable (id int)
DROP TABLE #tempTable

CREATE TABLE #tempTable (id int)
DROP TABLE #tempTable

From my understanding, in the second part, it should create the #tempTable.

But it shows the following error

Msg 2714, Level 16, State 1, Line 4
There is already an object named '#tempTable' in the database.

I have searched for the reason and found that it is because of a GO statement between the two part of the query. Therefore, the correct query is

CREATE TABLE #tempTable (id int)
DROP TABLE #tempTable
GO    

CREATE TABLE #tempTable (id int)
DROP TABLE #tempTable

I have also found that GO just tells SSMS to send the SQL statements between each GO in individual batches sequentially.

My question is, how are SQL statements executed? Is it not executed sequentially?

If it executes sequentially, then why does my first query cause an error?

like image 282
Noor A Shuvo Avatar asked May 11 '17 10:05

Noor A Shuvo


People also ask

Do you need to use go in SQL?

They're not strictly required - they're just instructions for the SQL Server Management Studio to execute the statements up to this point now and then keep on going. GO is not a T-SQL keyword or anything - it's just an instruction that works in SSMS.

What is the purpose of the GO command in SQL Server?

GO is not a SQL keyword. It's a batch separator used by the SQL Server Management Studio code editor tool for when more than one SQL Statement is entered in the Query window. Then Go separates the SQL statements. We can say that Go is used as a separator between transact SQL Statements.

How SQL statements are executed?

In order to execute an SQL statement, you must first prepare the SQL statement. During preparation, the database will usually precompile the SQL statement and creates an access plan for the statement. The access plan is kept as long as the statement exists. You can then execute the statement as many times as you want.

What is the purpose of the GO batch separator?

You can use the GO command to separate batches of SQL commands in database scripts. The GO command executes all the SQL statements in the current batch. A batch is defined as all the SQL commands since the previous GO command, or since the start of the script if this is the first GO command in the script.


2 Answers

The SQL Server documentation does a pretty good job of explaining this.

In your particular case, the issue is compile-time errors versus execution-time errors.

How does this work? Without a GO separating the statements, all are compiled at the same time. The problem is that the third statement is a CREATE TABLE statement and the table already exists. All that is happened is that the statements are parsed and compiled.

With the GO, the first two statements are compiled and executed. Voila! There is no table for the CREATE in the third statement.

like image 70
Gordon Linoff Avatar answered Nov 04 '22 12:11

Gordon Linoff


The main reason of this error is that parsing the query occurs before executing the query.

It happens because the whole code is executed as one batch. And SQL Server has one task to parse and execute.

That's why a GO command (not a statement) allows you to avoid this problem. It signals the end of a batch.

Here is a good topic to read about it: Understanding how SQL Server executes a query

like image 21
Paweł Tajs Avatar answered Nov 04 '22 12:11

Paweł Tajs