SQL “GO” Command Explained

The first thing that we need to know is that the GO keyword is not really a function in SQL. The sole purpose of its existence is that it acts as the default keyword in certain SQL management studios, used to separate batches of scripts. In other words, it enforces the execution precedence of SQL scripts. Also, the keyword that is used for batch separation can also be changed in the respective management studios, and it does not necessarily have to be GO. The GO keyword can be used in one of two ways. The first way is to use it to CREATE something first before writing a dependent query that follows after it. An excellent example of that is when you create a function, a rule, or a view. To use those user-defined functions, rules, or views, you will have to create them first because you cannot use them in a query before it exists. In this case, you will use the GO statement to separate the creation and query portion of that script so that it executes the create command first, then execute the following query command second. Another way to use the GO keyword is if you want to run a particular set of a script multiple times. When you input an integer value behind the GO keyword, the SQL server recognizes that integer value as the number of cycles and executes it repeatedly until that cycle threshold is met. This is useful because it can be used to substitute coding a looping script and prevents the possibility of infinite loops.

Leave a comment