sqlchild
sqlchild

Reputation: 9064

difference in executing a single line query with and without GO keyword

what's the difference in executing below query with and without GO keyword

use hello


use hello
GO

i am using use database query, it could be insert, select or any query.

Upvotes: 0

Views: 411

Answers (3)

Thomas
Thomas

Reputation: 64635

It depends on how it is executed. If you simply execute use hello in SSMS, it is equivalent to

use hello;
GO

I.e., when you execute a statement by itself, it is being executed as its own batch. However, if you execute the following SSMS:

use hello;
GO
Select 'hello'

, you are executing two batches which are separated by the GO keyword. From C#, outside of using the SQL Server SMO, you cannot execute a script with multiple batches using a Command object (e.g. SqlCommand). I.e., when you use a Command object from C#, it cannot contain the GO keyword.

Upvotes: 2

Bryan S.
Bryan S.

Reputation: 1444

GO isn't actually part of the query for SQL. Tools like SSMS use it to know in a block of statements to send them individidually. So think of GO as a seperator.

So..

SELECT ...

GO

SELECT ...

Will be sent to SQL as two seperate commands and not just one.

Upvotes: 4

Related Questions