SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Technical used of GO statement


Technical used of GO statement

Author
Message
Shadab Shah
Shadab Shah
SSC-Addicted
SSC-Addicted (443 reputation)SSC-Addicted (443 reputation)SSC-Addicted (443 reputation)SSC-Addicted (443 reputation)SSC-Addicted (443 reputation)SSC-Addicted (443 reputation)SSC-Addicted (443 reputation)SSC-Addicted (443 reputation)

Group: General Forum Members
Points: 443 Visits: 798
Hi all,

I am relatively new to SQL Server. I have seen that at the end of various statement we give GO. So my question is the query would get executed if we give GO on the other side it would also get executed if we did not provide GO. So whats the technical used of GO. Is it used for maintaining some Standard
anthony.green
anthony.green
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10098 Visits: 6336
GO is a batch sperator.

So you could say

Do Something
Do Something Else
Do Something More
GO
Do Something Even More
Do Something Better Than Before
GO

And it would execute the first three things in one batch, and the last two things in another batch.

Without the GO's it would execute all 5 things in one batch



Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
When a question, really isn't a question - Jeff Smith
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger


Shadab Shah
Shadab Shah
SSC-Addicted
SSC-Addicted (443 reputation)SSC-Addicted (443 reputation)SSC-Addicted (443 reputation)SSC-Addicted (443 reputation)SSC-Addicted (443 reputation)SSC-Addicted (443 reputation)SSC-Addicted (443 reputation)SSC-Addicted (443 reputation)

Group: General Forum Members
Points: 443 Visits: 798
anthony.green (12/4/2012)
GO is a batch sperator.

So you could say

Do Something
Do Something Else
Do Something More
GO
Do Something Even More
Do Something Better Than Before
GO

And it would execute the first three things in one batch, and the last two things in another batch.

Without the GO's it would execute all 5 things in one batch

Thanks antony for the reply but since i am relatively new to SQL Server, i was wondering in what scenario would we like the above mention thing to happen. I mean to say there is no conditional break and my objective is to excute all the 5 statement, so to me executing them one after another in sequence matter, why should i used GO.
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26121 Visits: 17539
Shadab Shah (12/4/2012)
anthony.green (12/4/2012)
GO is a batch sperator.

So you could say

Do Something
Do Something Else
Do Something More
GO
Do Something Even More
Do Something Better Than Before
GO

And it would execute the first three things in one batch, and the last two things in another batch.

Without the GO's it would execute all 5 things in one batch

Thanks antony for the reply but since i am relatively new to SQL Server, i was wondering in what scenario would we like the above mention thing to happen. I mean to say there is no conditional break and my objective is to excute all the 5 statement, so to me executing them one after another in sequence matter, why should i used GO.


GO is ONLY used in SSMS. It is NOT a t-sql statement. It is a way to control what batches run. Think of a batch as something you would execute from another program. Consider the following code.


declare @MyInt int = 42

select @MyInt

Select @MyInt / 2

GO

select @MyInt



This all looks perfectly fine. Try running it. The last select statement is after the GO batch separator. That means the variable @MyInt is not in the current batch. The above will return 2 rows and error on the last select.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Shadab Shah
Shadab Shah
SSC-Addicted
SSC-Addicted (443 reputation)SSC-Addicted (443 reputation)SSC-Addicted (443 reputation)SSC-Addicted (443 reputation)SSC-Addicted (443 reputation)SSC-Addicted (443 reputation)SSC-Addicted (443 reputation)SSC-Addicted (443 reputation)

Group: General Forum Members
Points: 443 Visits: 798
Sean Lange (12/4/2012)
Shadab Shah (12/4/2012)
anthony.green (12/4/2012)
GO is a batch sperator.

So you could say

Do Something
Do Something Else
Do Something More
GO
Do Something Even More
Do Something Better Than Before
GO

And it would execute the first three things in one batch, and the last two things in another batch.

Without the GO's it would execute all 5 things in one batch

Thanks antony for the reply but since i am relatively new to SQL Server, i was wondering in what scenario would we like the above mention thing to happen. I mean to say there is no conditional break and my objective is to excute all the 5 statement, so to me executing them one after another in sequence matter, why should i used GO.


GO is ONLY used in SSMS. It is NOT a t-sql statement. It is a way to control what batches run. Think of a batch as something you would execute from another program. Consider the following code.


declare @MyInt int = 42

select @MyInt

Select @MyInt / 2

GO

select @MyInt



This all looks perfectly fine. Try running it. The last select statement is after the GO batch separator. That means the variable @MyInt is not in the current batch. The above will return 2 rows and error on the last select.


Thanks sean that was indeed a very good example, cleared the concept clearly but has anybody used this in real application. I have never seen such GO written in between the lines of code in SP or in that matter in any other script
Sinfonico25
Sinfonico25
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 167
You must be very careful using the Batch Separator Sentence: GO

If the previous batch failed, the result will be an error message, and the next batch will be executed. Example:
USE TestDB
GO
DELETE FROM dbo.TableName_In_TestServer_AND_ProductionServer
GO



If you execute the previous query on Test Server Instance, it will be ok, but if you execute it on Production Server Instance it will be a serious pain to you: An error message will be shown because TestDB does not exists on Production Server on the first batch and the Delete Batch will be executed.

A peculiarity is that GO cannot be on the same line of any query and throws an error message:
DELETE FROM dbo.TableName_In_TestServer_AND_ProductionServer GO



And you could not use a variable declared on a previous batch:

DECLARE @Var VARCHAR(10)
SET @Var = 'Hello'
GO

Print @Var
GO


Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26121 Visits: 17539
It would not work inside a stored proc. It would end the batch. Smile

For example if you have a script that create a stored proc and a view you HAVE to use a batch separator.

Take a look at this.


create proc MyProc as
begin

select 1
end

create view MyView as
(
select top 5 * from sys.columns
)



These are obviously greatly simplified but serves the point. This script won't run and you will get an "incorrect syntax" exception. If you mouse over the red squiggly line for the create view line it will tell that "CREATE VIEW must be the only statement in the batch".

So to save us the hassle of making us open every single statement in a new window we have a batch separator. Here is the same proc and view but I added GO so it will run. I also added drop statements at the end.


create proc MyProc as
begin

select 1
end

GO

create view MyView as
(
select top 5 * from sys.columns
)

GO

drop procedure MyProc
drop view MyView



This type of thing is often used for deployment scripts or other times you need to execute multiple batches at one time.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search