I have seen people use GO statement between batches of SQL code, but AFAICS it is not mandatory (SQL Server 2008). What are the benefits using GO statements between batches/sets of SQL statements?
相关问题
- SQL join to get the cartesian product of 2 columns
- sql execution latency when assign to a variable
- Difference between Types.INTEGER and Types.NULL in
- What is the best way to cache a table from a (SQL)
- php PDO::FETCH_ASSOC doesnt detect select after ba
It's only mandatory in SQL tools to tell SSMS where the batch start and end is. It's also required for some statements such as CREATE TRIGGER which must the first in the batch
For example, in a c# to SQL Server call it has no meaning
They're not strictly required - they're just instructions for the SQL Server Management Studio to execute the statements up to this point now and then keep on going.
GO
is not a T-SQL keyword or anything - it's just an instruction that works in SSMS.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.
E.g. if you try to execute this, you'll get errors from SSMS:
Results in:
The point is: SSMS is trying to verify the whole statement at once, but on the SELECT statement, it will complain about the missing
DateTimeStamp
column.If you put a
GO
between the two statements, it'll work, because SSMS won't parse and verify the whole statement ahead of time - it will do the first part, and then only parse the second (after theGO
).But other than situations like this one, GO is hardly ever required.