I use ADO.NET or the sqlcmd utility to send sql scripts to SQL 2008. What is the difference between using ";" and "GO" to separate chunks of SQL? Thanks, NEstor
semicolon is a statement separator. The previous statement(s) is not necessarily executed when a semicolon is encountered.
GO
Signifies the end of a batch. Executes the previous batch of statements, as does encountering the end of the block.
GO 2
Means execute the batch that many times. I think I've used that option maybe twice in my life. Then again, I'm not a DBA by trade.
I thought the ; character separates a list of SQL commands, GO just instructs SQL Server to commit all the previous commands.
'GO' is typically used to indicate the end of a batch of SQL statements which means that you could have a begin transaction
and end transaction
wrapped up into a single collection of statements that could fail or succeed together.
';' is generally used to separate multiple SQL statements from one another. This is noticable in SQL scripts that need to return multiple recordsets, such as `select * from table1; select * from table2;' which would result in two separate recordsets on the client's side.
"GO" is similar to ; in many cases, but does in fact signify the end of a batch.
Each batch is committed when the "GO" statement is called, so if you have:
SELECT * FROM table-that-does-not-exist;
SELECT * FROM good-table;
in your batch, then the good-table select will never get called because the first select will cause an error.
If you instead had:
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 committing a transaction.
GO is not actually a T-SQL command, it was something introduced by the tools as a way to separate batch statements such as the end of a stored procedure. It is supported by the MS SQL stack tools but not formally part of other tools.
You cannot put a GO into a string of SQL and send it as part of a ADO.NET command object as SQL itself doesnt understand the term. Another way to demonstrate this is with the profiler, setup some statements that use GO in Query Analyzer/Management Studio and then run the profiler when you execute - you will see they are issued as separate commands to the server.
The semi-colon is used to signify the end of a statement itself, not necessarily a whole batch.