What does sqlgo do?

196    Asked by ClareMatthews in SQL Server , Asked on Mar 13, 2023

The 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?

Answered by David EDWARDS
It splits the command into individual batches.

For instance, variables' scoped ends with sqlgo.
; separates commands but doesn't end scope.
GO separates batches and ends scope.

You may think of it as if the whole command got split by GO and each part got executed per se.

ie.
This works:
declare @a int;
set @a = 1;
select @a
But this doesn't work:
declare @a int
set @a = 1
GO
select @a -- will throw error as variable is not declared


Your Answer

Interviews

Parent Categories