Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Technical used of GO statement Expand / Collapse
Author
Message
Posted Tuesday, December 4, 2012 7:43 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, October 13, 2014 4:08 AM
Points: 268, Visits: 670
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
Post #1392493
Posted Tuesday, December 4, 2012 8:02 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Monday, October 13, 2014 6:42 AM
Points: 5,216, Visits: 5,111
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
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

Post #1392514
Posted Tuesday, December 4, 2012 8:10 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, October 13, 2014 4:08 AM
Points: 268, Visits: 670
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.
Post #1392520
Posted Tuesday, December 4, 2012 8:14 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 8:56 PM
Points: 13,078, Visits: 12,529
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 Moden's 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)
Post #1392522
Posted Tuesday, December 4, 2012 8:20 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, October 13, 2014 4:08 AM
Points: 268, Visits: 670
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
Post #1392526
Posted Tuesday, December 4, 2012 8:25 AM


Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, January 7, 2014 3:20 PM
Points: 13, Visits: 158
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

Post #1392533
Posted Tuesday, December 4, 2012 8:28 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 8:56 PM
Points: 13,078, Visits: 12,529
It would not work inside a stored proc. It would end the batch. :)

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 Moden's 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)
Post #1392535
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse