2

I have read up about the GO command but I have not seen a clear answer to the following question.

Let's say I run the following set of commands from the SSMS:

INSERT INTO [dbo].[hwr] ([wr_id]) VALUES (990017882)
INSERT INTO [dbo].[hwr] ([wr_id]) VALUES (990017883)
INSERT INTO [dbo].[hwr] ([wr_id]) VALUES (990017884)
INSERT INTO [dbo].[hwr] ([wr_id]) VALUES (990017885)

How does this differ from:

INSERT INTO [dbo].[hwr] ([wr_id]) VALUES (990017882)
GO
INSERT INTO [dbo].[hwr] ([wr_id]) VALUES (990017883)
GO
INSERT INTO [dbo].[hwr] ([wr_id]) VALUES (990017884)
GO
INSERT INTO [dbo].[hwr] ([wr_id]) VALUES (990017885)
GO

In other words, is the 'GO' command actually accomplishing anything in this situation? If so, what?

Many thanks, Matt

user2864740
  • 60,010
  • 15
  • 145
  • 220
user2135970
  • 795
  • 2
  • 9
  • 22

2 Answers2

3

The GO command is actually not a SQL command, but an SSMS command. It tells SSMS to send the previous commands as a batch.

So the difference is your first code will send it all at once, while the second will send in four batches.

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
3

GO is a batch separator

Certain operations require it to be the first line in a batch (CREATE PROCEDURE) for example. If you use GO you then can have all the commands in 1 file

Also you can declare the same variable if it is in its own batch

example

declare @i int = 5
select @i
go
declare @i int = 5
select @i
go

Take out the go and it will blow up

You can also add a number after GO and it will execute the batch that many times, example

declare @i int = 5
select @i
go 4
SQLMenace
  • 132,095
  • 25
  • 206
  • 225