xhr489
xhr489

Reputation: 2309

T-SQL, why do we write GO

Why are there two GO's here in this code? When the query only has to run once why do we need to include it?

USE AdventureWorks2016;
GO

SELECT 
    Ord.SalesOrderID, Ord.OrderDate,
    (SELECT MAX(OrdDet.UnitPrice)
     FROM Sales.SalesOrderDetail AS OrdDet
     WHERE Ord.SalesOrderID = OrdDet.SalesOrderID) AS MaxUnitPrice
FROM Sales.SalesOrderHeader AS Ord;
GO

Upvotes: 0

Views: 98

Answers (1)

John Cappelletti
John Cappelletti

Reputation: 81940

If it helps, GO isolates each batch

For example

Declare @v int = 25
Select @V;

go

Select @v ;

The 1st SELECT executes fine and returns 25 while the 2nd throws an error

Upvotes: 2

Related Questions