{"id":5552,"date":"2016-10-10T22:06:02","date_gmt":"2016-10-10T16:36:02","guid":{"rendered":"http:\/\/sqlhints.com\/?p=5552"},"modified":"2016-10-10T22:06:02","modified_gmt":"2016-10-10T16:36:02","slug":"go-statement-in-sql-server","status":"publish","type":"post","link":"https:\/\/sqlhints.com\/2016\/10\/10\/go-statement-in-sql-server\/","title":{"rendered":"GO Statement in Sql Server"},"content":{"rendered":"<p style=\"text-align: justify;\">GO statement is used as a Batch separator in Sql Server. Batch is nothing but one or more Sql Server statements sent to the Sql Server engine as one set of statements.<\/p>\n<p style=\"text-align: justify;\">GO is not a Transact-SQL statement, instead it is a command recognized by the Sql Server Management Studio (i.e. SSMS), SQLCMD and OSQL utilities. These utilities send all statements after the previous GO statement and before the current GO statement as one Batch to the Sql Server engine for execution. So, it means everything in that batch is local to that batch. In other words any variables declared in the current batch will not be visible in the next batch (i.e. variables declared before the GO statement are not accessible after the GO statement).<\/p>\n<p style=\"text-align: justify;\">GO Statement can also be used to execute batch of T-Sql statement multiple times. Let us understand GO statement with extensive list of examples:<\/p>\n<h3>Example 1: GO Statement the Batch Separator example<\/h3>\n<pre class=\"brush: sql; gutter: false\">\r\nDECLARE @Name NVARCHAR(50) = &#039;Basavaraj Biradar&#039;\r\nSELECT @Name AS &#039;Name&#039;\r\nGO\r\nDECLARE @Name NVARCHAR(50) = &#039;Shreeganesh Biradar&#039;\r\nSELECT @Name AS &#039;Name&#039;\r\n<\/pre>\n<p><strong>RESULT:<\/strong><br \/>\n<a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2016\/10\/Sql-GO-Statement-Batch-Separator.jpg\" rel=\"attachment wp-att-5553\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2016\/10\/Sql-GO-Statement-Batch-Separator.jpg\" alt=\"sql-go-statement-batch-separator\" width=\"630\" height=\"632\" class=\"alignnone size-full wp-image-5553\" srcset=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2016\/10\/Sql-GO-Statement-Batch-Separator.jpg 630w, https:\/\/sqlhints.com\/wp-content\/uploads\/2016\/10\/Sql-GO-Statement-Batch-Separator-150x150.jpg 150w, https:\/\/sqlhints.com\/wp-content\/uploads\/2016\/10\/Sql-GO-Statement-Batch-Separator-300x300.jpg 300w\" sizes=\"auto, (max-width: 630px) 100vw, 630px\" \/><\/a><\/p>\n<p style=\"text-align: justify;\">Even though here we see that the same variable @Name is declared twice, but it is still working. Reason is between these two variable declarations we have a batch separator GO statement. Variables declared before the GO statement are not accessible after the GO statement. Basically SSMS sends the first batch (i.e. Batch 1) of statements to the SQL Engine first, once its execution is over it sends the second batch of statements (i.e. Batch 2) after the GO statement to the SQL Engine for execution.<\/p>\n<h3>Example 2: Execute batch of T-Sql statements multiple times in SSMS<\/h3>\n<p style=\"text-align: justify;\">GO statement also has an integer optional parameter, this parameter value signals Sql Server to execute the batch of T-Sql Statement prior to the GO statement to be executed for the specified number of times. Let us understand this with following example:<\/p>\n<pre class=\"brush: sql; gutter: false\">\r\nPRINT &#039;Hello&#039;\r\nGO 5\r\n<\/pre>\n<p><strong>RESULT:<\/strong><br \/>\n<a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2016\/10\/SQL-GO-to-execute-Batch-of-T-Sql-Statements-multiple-times.jpg\" rel=\"attachment wp-att-5554\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2016\/10\/SQL-GO-to-execute-Batch-of-T-Sql-Statements-multiple-times.jpg\" alt=\"sql-go-to-execute-batch-of-t-sql-statements-multiple-times\" width=\"397\" height=\"272\" class=\"alignnone size-full wp-image-5554\" srcset=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2016\/10\/SQL-GO-to-execute-Batch-of-T-Sql-Statements-multiple-times.jpg 397w, https:\/\/sqlhints.com\/wp-content\/uploads\/2016\/10\/SQL-GO-to-execute-Batch-of-T-Sql-Statements-multiple-times-300x206.jpg 300w\" sizes=\"auto, (max-width: 397px) 100vw, 397px\" \/><\/a><\/p>\n<h3>Example 3: T-Sql statements shouldn\u2019t be on the same line as that of the GO Statement<\/h3>\n<pre class=\"brush: sql; gutter: false\">\r\nGO SELECT &#039;Basavaraj Biradar&#039;\r\n<\/pre>\n<p><strong>RESULT:<\/strong><\/p>\n<p style=\"text-align: justify;color: red;\">\nA fatal scripting error occurred.<br \/>\nIncorrect syntax was encountered while parsing GO.\n<\/p>\n<p style=\"text-align: justify;\">To avoid this issue, we can re-write the above example as below:<\/p>\n<pre class=\"brush: sql; gutter: false\">\r\nGO \r\nSELECT &#039;Basavaraj Biradar&#039;\r\n<\/pre>\n<p><strong>RESULT:<\/strong><br \/>\n<a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2016\/10\/Transact-Sql-Statement-shouldnt-be-on-the-same-line-as-GO-statement.jpg\" rel=\"attachment wp-att-5555\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2016\/10\/Transact-Sql-Statement-shouldnt-be-on-the-same-line-as-GO-statement.jpg\" alt=\"transact-sql-statement-shouldnt-be-on-the-same-line-as-go-statement\" width=\"356\" height=\"177\" class=\"alignnone size-full wp-image-5555\" srcset=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2016\/10\/Transact-Sql-Statement-shouldnt-be-on-the-same-line-as-GO-statement.jpg 356w, https:\/\/sqlhints.com\/wp-content\/uploads\/2016\/10\/Transact-Sql-Statement-shouldnt-be-on-the-same-line-as-GO-statement-300x149.jpg 300w\" sizes=\"auto, (max-width: 356px) 100vw, 356px\" \/><\/a><\/p>\n<h3>Example 4: Comment can be on the same line as GO statement<\/h3>\n<p style=\"text-align: justify;\">We can write comment on the same line as that of the GO statement<\/p>\n<pre class=\"brush: sql; gutter: false\">\r\nGO  --Comment can be on the same line as GO\r\n<\/pre>\n<p><strong>RESULT:<\/strong><br \/>\n<a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2016\/10\/SQL-GO-Comment-can-be-on-the-same-line-as-GO-statement.jpg\" rel=\"attachment wp-att-5556\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2016\/10\/SQL-GO-Comment-can-be-on-the-same-line-as-GO-statement.jpg\" alt=\"sql-go-comment-can-be-on-the-same-line-as-go-statement\" width=\"473\" height=\"120\" class=\"alignnone size-full wp-image-5556\" srcset=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2016\/10\/SQL-GO-Comment-can-be-on-the-same-line-as-GO-statement.jpg 473w, https:\/\/sqlhints.com\/wp-content\/uploads\/2016\/10\/SQL-GO-Comment-can-be-on-the-same-line-as-GO-statement-300x76.jpg 300w\" sizes=\"auto, (max-width: 473px) 100vw, 473px\" \/><\/a><\/p>\n<h3>Example 5: GO statement can\u2019t be part of the definition of the Stored Procedure, Function, View etc<\/h3>\n<p style=\"text-align: justify;\">We can\u2019t add a GO statement in the definition of the Stored Procedure\/Function\/View etc<\/p>\n<pre class=\"brush: sql; gutter: false\">\r\nCREATE PROCEDURE GOStatementDemo\r\nAS\r\nBEGIN\r\n\tSELECT &#039;Basavaraj Biradar&#039;\r\n\tGO\t\r\n\tSELECT &#039;Shreeganesh Biradar&#039;\r\nEND\r\n<\/pre>\n<p><strong>RESULT:<\/strong><\/p>\n<p style=\"text-align: justify;color: red;\">\nMsg 102, Level 15, State 1, Procedure GOStatementDemo, Line 4 [Batch Start Line 0]<br \/>\nIncorrect syntax near &#8216;Basavaraj Biradar&#8217;.<br \/>\nMsg 102, Level 15, State 1, Line 7<br \/>\nIncorrect syntax near &#8216;END&#8217;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>GO statement is used as a Batch separator in Sql Server. Batch is nothing but one or more Sql Server statements sent to the Sql Server engine as one set of statements. GO is not a Transact-SQL statement, instead it is a command recognized by the Sql Server Management Studio (i.e. SSMS), SQLCMD and OSQL &hellip; <a href=\"https:\/\/sqlhints.com\/2016\/10\/10\/go-statement-in-sql-server\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">GO Statement in Sql Server<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[3],"tags":[1360,1364,1359,1357,1358,1361,1356,1366,32,1362,1363,321,1365,986,1355],"class_list":["post-5552","post","type-post","status-publish","format-standard","hentry","category-sql-server","tag-batch-separator-in-sql","tag-examples-of-sql-go-statement","tag-go-batch-separator-in-sql","tag-go-in-sql","tag-go-in-sql-server","tag-go-loop","tag-go-sql-server","tag-go-statement-example","tag-go-statement-in-sql-server","tag-go-statement-to-execute-batch-of-statements-multiple-times","tag-go-statement-tutorial","tag-sql","tag-sql-go-statement-examples","tag-sql-server","tag-sql-server-go"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3xNAz-1ry","_links":{"self":[{"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/posts\/5552","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/comments?post=5552"}],"version-history":[{"count":1,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/posts\/5552\/revisions"}],"predecessor-version":[{"id":5557,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/posts\/5552\/revisions\/5557"}],"wp:attachment":[{"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/media?parent=5552"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/categories?post=5552"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/tags?post=5552"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}