title: t-SQL: Incorrect Syntax Near 'Go'
body: <blockquote>
    <p><em>note: I have already reviewed several similar questions both on SO and on other forums, none of the suggestions have proven useful.</em></p>
  </blockquote>
  
  <p>The following t-SQL script works just fine:</p>
  
  <pre><code>USE [MASTER]
  GO
  BEGIN
  IF NOT EXISTS 
      (SELECT name  
       FROM master.sys.server_principals
       WHERE name = 'myTestUser')
  BEGIN
      PRINT 'CREATING LOGIN: [myTestUser]'
      CREATE LOGIN [myTestUser] WITH PASSWORD = N'P@55w0rd'
  END ELSE BEGIN
      PRINT 'LOGIN [myTestUser] already exists on this server.'
          END
  END
  GO
  BEGIN
  IF NOT EXISTS 
          (
              SELECT 1 
              FROM master.dbo.sysdatabases 
              WHERE name ='myTestDb' 
          )
          BEGIN
              PRINT 'CREATING DATABASE [myTestDb]'
              --INSERT CREATE DB SCRIPT --
          END ELSE BEGIN      
              PRINT 'DATABASE [myTestDb] already exists on this server'
          END;
  END
  GO
  USE[myTestDb]
  </code></pre>
  
  <p>The following script <strong>ALSO</strong> works fine:</p>
  
  <pre><code>use [master]
  CREATE DATABASE [myTestDb]
   CONTAINMENT = NONE
   ON  PRIMARY 
  ( NAME = N'myTestDb', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\myTestDb.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
   LOG ON 
  ( NAME = N'myTestDb_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\myTestDb_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
  GO
  --I have several ALTER DATABASE commands to follow, this is the minimum    
  --necessary to duplicate the error condition.
  </code></pre>
  
  <p>HOWEVER When the second script is dropped inside the first script <em>(less the USE [master] line)</em> at the point where it reads: --INSERT CREATE DB SCRIPT-- I get </p>
  
  <blockquote>
    <p>Incorrect Syntax Near 'Go'</p>
  </blockquote>
  
  <p>on the first 'Go'...
  If I remove the 'Go' the error moves to the next instance of 'Go'..
  The LAST 'Go' is critical (before the new USE)</p>