What's the difference between ";" and "GO" in T-SQL?

I use ADO.NET as well as sqlcmd utility to send SQL scripts to SQL Server 2008. What is the difference between using ; and GO to split pieces of SQL?

+39
sql sql-server tsql sql-server-2008
04 Oct '09 at 22:14
source share
7 answers

GO is not really a T-SQL command. GO was introduced by Microsoft tools as a way to separate batch commands, such as the end of a stored procedure. GO supported by the Microsoft SQL stack tools, but is not formally part of other tools.

You cannot put GO in an SQL string and send it as part of an ADO.NET command object, because SQL itself does not understand this term. Another way to demonstrate this is with the profiler: configure some statements that use GO in Query Analyzer / Management Studio, and then run the profiler at runtime. You will see that they are issued as separate commands to the server.

A semi-colon is used to mark the end of the statement itself, not necessarily the whole lot.

http://msdn.microsoft.com/en-us/library/ms188037.aspx

+50
04 Oct '09 at 22:25
source share

"GO" is similar; in many cases, but actually means the end of the party.

Each batch is executed when the "GO" statement is called, so if you have:

 SELECT * FROM table-that-does-not-exist; SELECT * FROM good-table; 

in your party, the choice of a good table will never be called, because the first choice will lead to an error.

If you have:

 SELECT * FROM table-that-does-not-exist GO SELECT * FROM good-table GO 

The first select statement still causes an error, but since the second statement is in its own batch, it will still execute.

GO has nothing to do with a transaction.

+27
04 Oct '09 at 22:21
source share

semicolon - operator separator. The previous statement is not necessarily executed when a semicolon is encountered.

 GO 

Indicates the end of the batch. Executes the previous batch of statements, just as it meets the end of a block.

 GO 2 

The tool performs the batch many times. I seem to have used this option, perhaps twice in my life. On the other hand, I am not a database administrator by profession.

+21
Oct 04 '09 at 10:17
source share

"GO" is usually used to indicate the end of a package of SQL statements, which means that you could have begin transaction and end transaction wrapped in a single set of statements that could fail or succeed together.

';' commonly used to separate multiple SQL statements from each other. This can be seen in SQL scripts that must return multiple recordsets, such as `select * from table1; select * from table2; 'which will lead to the creation of two separate recordsets on the client side.

+4
04 Oct '09 at 10:17
source share
  • In SQL Server TSQL (2005 - 2016), remember that:

    • The semicolon (;) is the block terminator.
    • GO is a party terminator.
  • In addition, GO can be used to invoke the same DML block simultaneously using the following syntax:

GO [count]

Where [count] is a positive integer indicating how many times the TSQL block of instructions preceding GO must be executed again and again.

  1. In addition, unlike a semicolon, GO is mandatory before a new DDL, say, when creating a new view, because the semicolon separating previous commands causes an error. For example:

drop view #temporary_view
GO
create view #another_view ...
β†’ NO ERRORS

If you replaced GO with a semicolon in the previous example, this will result in the following error message:

'CREATE VIEW' must be the first statement in the query package.

+2
Aug 25 '15 at 20:16
source share

The GO command means the end of the game. Therefore, all variables declared before GO are invalid after the GO command. Against the semicolon, the packet does not end.

If you will use the DML command in a procedure, use a semicolon instead of GO. For example:

 CREATE PROCEDURE SpMyProc @myProcParam VARCHAR(20) AS DECLARE @myOtherParam INT = 5 ;DISABLE TRIGGER ALL ON tMyTable UPDATE tMyTable SET myVar = @myProcParam, mySecondVar = @myOtherParam ;ENABLE TRIGGER OLL ON tMyTable 
+1
Feb 12 '15 at 10:00
source share

I thought; character separates the list of SQL commands; GO simply instructs SQL Server to execute all previous commands.

0
04 Oct '09 at 10:17
source share



All Articles