Home > Tips N Tricks > GO ‘N’ Times

GO ‘N’ Times

One unbelievable tip for the day for T-SQL users.  What a “GO” statement do in T-SQL.

By definition it is a batch separator. It will run a batch and commit it. But along with that it also accepts one parameter which tells the SQL engine basically how many times to do the job.

For Example,

SELECT * FROM   sys.objects;
GO 3

It will show the output THREE times  :)

Next Example,

IF EXISTS (SELECT 1
           FROM   tempdb.sys.objects
           WHERE  name LIKE '%#ProductTable%')
    DROP TABLE #ProductTable;
 
CREATE TABLE #ProductTable
(
    ID   INT          IDENTITY (1, 1),
    Name VARCHAR (50)
);
 
 
GO
INSERT  INTO #ProductTable (Name)
VALUES                    ('Office XP');
 
GO 3
 
SELECT *
FROM   #ProductTable;

OUTPUT

Note: “GO” is the keyword used by SSMS to separate query batches. It can be configured in this location:

Tools » Options » Query Execution » SQL Server » General. Change the value for “Batch Separator:” in the right pane. To see the immediate effect of this change, you need to close all query windows in the current session and open new query window.








Categories: Tips N Tricks
  1. Roseanna
    September 13th, 2011 at 02:24 | #1

    No question this is the place to get this info, thanks y’all.

  2. Marina
    September 17th, 2011 at 22:37 | #2

    You have really interesting blog, keep up posting such informative posts!

  3. Daniel
    September 20th, 2011 at 22:23 | #3

    Much appreciated for the information and share!

  4. Блог о путешествиях
    September 26th, 2011 at 23:45 | #4

    Thx for this great information that you are sharing with us!!!

  5. October 1st, 2011 at 10:27 | #5

    Thanks for the share!
    Nancy.R

  1. No trackbacks yet.