What is the use of GO in Transact SQL?

Technology CommunityCategory: T-SQLWhat is the use of GO in Transact SQL?
VietMX Staff asked 3 years ago

The GO command isn’t a Transact-SQL statement, but a special command recognized by several MS utilities including SQL Server Management Studio code editor.

The GO command is used to group SQL commands into batches which are sent to the server together. The commands included in the batch, that is, the set of commands since the last GO command or the start of the session, must be logically consistent. For example, you can’t define a variable in one batch and then use it in another since the scope of the variable is limited to the batch in which it’s defined.

Sometimes, you need a GO – e.g. if you add a column to a table, and then want to select it again, you need to have a GO between the adding of the column, and the query of it. The point is SSMS is trying to verify the whole statement at once, but on the SELECT statement, it will complain about the missing selected column.