patternsqlCritical
What does the GO statement do in SQL Server?
Viewed 0 times
thewhatstatementsqldoesserver
Problem
The
I noticed that queries with or without
And what is the difference between semicolons
GO statement from SQL Server caused me great curiosity and I don't really know how to use it properly.I noticed that queries with or without
GO don't return errors and seem to work the same, so what is the purpose of it and why should I use it?And what is the difference between semicolons
; and GO at the end of a query?Solution
GO is not a part of the TSQL language. It's a batch separator used by SQLCMD and SSMS.GO is not a Transact-SQL statement; it is a command recognized by the
sqlcmd and osql utilities and SQL Server Management Studio Code
editor.
SQL Server utilities interpret GO as a signal that they should send
the current batch of Transact-SQL statements to an instance of SQL
Server. The current batch of statements is composed of all statements
entered since the last GO, or since the start of the ad hoc session or
script if this is the first GO.
A Transact-SQL statement cannot occupy the same line as a GO command.
However, the line can contain comments.
SQL Server Utilities Statements - GO
It originated with the command line interfaces, and persists in sqlcmd to this day. You can write a batch over multiple lines, and the batch isn't sent to the server until you type
go.1> select name, create_date
2> from sys.objects
3> where name = 'foo'
4> go
name create_date
--------- -----------
foo 2020-07-20 09:56:42.393
(1 rows affected)
1>You can configure what to use for a batch separator in SSMS.
GO is the default, but it can be just about anything else.Code Snippets
1> select name, create_date
2> from sys.objects
3> where name = 'foo'
4> go
name create_date
--------- -----------
foo 2020-07-20 09:56:42.393
(1 rows affected)
1>Context
StackExchange Database Administrators Q#283022, answer score: 85
Revisions (0)
No revisions yet.