The GO keyword signals the completion of a package for SQL Server Management Studio - usually SQL Server Management Studio executes all statements in one package (the package can be considered as feedback to the database), but in some situations it may be necessary to execute statements in different batches (for example, the SET statement SHOWPLAN_ALL must be the only statement in the package)
For example, run the following script in SQL Server Management Studio:
USE StackOverflow GO SELECT * FROM Comments
It is roughly equivalent to doing the following in C #:
using (var cmd = new SqlCommand("USE StackOverflow", conn)) { cmd.ExecuteReader(); } using (var cmd = new SqlCommand("SELECT * FROM Comments", conn)) { cmd.ExecuteReader(); }
Note that GO not a T-SQL keyword; it is understood only by SQL Server Management Studio and other SQL tools. For example, the following work will not work and will throw an exception at runtime:
string cmdText = @" USE StackOverflow GO SELECT * FROM Comments"; using (var cmd = new SqlCommand(cmdText, conn)) { cmd.ExecuteReader(); }
Justin
source share