title: Creating and inserting into a DB using Dynamic SQL
body: <p>To whoever reads this,</p>
  
  <p>Basically using dynamic SQL, i am trying to create a database and then insert into it. Problem is that I cant find an alternative to 'GO' since when i run it i get an error saying the newly created DB doesn't exist. I know that it cant be used in dynamic sql as it is not recognized as T-SQL. I've also tried adding ";" but error still persists.</p>
  
  <pre><code>DECLARE @TargetDB sysname
  DECLARE @TargetSchema  sysname
  DECLARE @TargetTable sysname
  DECLARE @SourceDB sysname
  DECLARE @SourceSchema sysname
  DECLARE @SourceTable sysname
  DECLARE @sql NVARCHAR(max)
  
  SET @SourceDB = 'AYOOO'
  SET @TargetDB = @SourceDB + 'SandBox'
  SET @SourceTable = 'GUCCI'
  SET @TargetTable = @SourceTable + 'SandBox'
  
  SET @sql = N'CREATE DATABASE ' + @TargetDB + '; ' --create new db
  
  SET @sql = @sql +  N' SELECT * INTO ' + @TargetDB +'.dbo.'+@TargetTable+' FROM ' + @SourceDB+'.dbo.'+@SourceTable; --these 2 lines are for copying data into new tables
  PRINT @sql
  EXEC sys.sp_executesql @sql
  </code></pre>
  
  <p>Error:</p>
  
  <pre><code>Database 'AYOOOSandBox' does not exist.
  </code></pre>
  
  <p>I know its a stupid question but I'd like to find a good alternative to "GO" or a better practice for Dynamic SQL.</p>
  
  <p>Thanks</p>