1

Can I ask what is the purpose of using "GO" Statement in SQL Server.

I understand that it is kind of grouping statements into a batch. But my question is why we have to group statements into batch?

Say in Oracle database, we simply run statements one by one and separate it with a semi-colon (";"). Does the GO statement in SQL Statement having the same functionality as the ";" in Oracle?

Thanks for sharing some light here.

E. L.
  • 502
  • 3
  • 16
  • What in the documentation, about `GO` and batches did you have questions on? I assume you have read both[Batches of SQL Statements](https://learn.microsoft.com/en-us/sql/odbc/reference/develop-app/batches-of-sql-statements?view=sql-server-ver15) and [SQL Server Utilities Statements - GO](https://learn.microsoft.com/en-us/sql/t-sql/language-elements/sql-server-utilities-statements-go?view=sql-server-ver15), so what didn't explain what you wanted to know? – Thom A Mar 06 '20 at 22:19
  • Take a peek at https://stackoverflow.com/questions/60243544/t-sql-why-do-we-write-go/60243947#60243947 – John Cappelletti Mar 06 '20 at 22:23

2 Answers2

1

Potential duplicate of https://dba.stackexchange.com/questions/59975/go-after-every-t-sql-statement specifically this answer https://dba.stackexchange.com/a/59976

To be more explicit, here is the relevant quote:

A batch is a group of one or more Transact-SQL statements sent at the same time from an application to SQL Server for execution.

That was a bunch of gooble dee gook but here's what I understand: Sometimes I can only have only one thing in a batch and if I want to put multiple things in the same SQL file, I need to surround that statement with GO.

For example, when you create a view in SQL server,

"CREATE VIEW must be the only statement in the batch"

https://stackoverflow.com/a/27272239/8250038

a student
  • 32
  • 5
  • So does it means there is a difference between Oracle SQL Developer and SQL Server Management Studio when executing SQL files with multiple statements. Oracle SQL Developer will sent through all of them one after finishing of executing each other, while SQL Server will sent them all together if without the GO Statement? – E. L. Mar 06 '20 at 22:38
  • I don't know but looks like what you're looking for is a slash https://stackoverflow.com/a/29961618/8250038 – a student Mar 08 '20 at 11:44
1

GO is a clear delineater between a set of commands. Its a bit like saying 'forget everything else, finish what you started, and prepare for something new'. All variables will be forgotten, and a new batch of commands are commenced.

See here https://sqlhints.com/tag/go-in-sql-server/

Grantly
  • 2,546
  • 2
  • 21
  • 31