Is there a way to persist a variable across a go?
Declare @bob as varchar(50);
Set @bob = 'SweetDB';
GO
USE @bob --- see note below
GO
INSERT INTO @bob.[dbo].[ProjectVersion] ([DB_Name], [Script]) VALUES (@bob,'1.2')
See this SO question for the ‘USE @bob’ line.
The
gocommand is used to split code into separate batches. If that is exactly what you want to do, then you should use it, but it means that the batches are actually separate, and you can’t share variables between them.In your case the solution is simple; you can just remove the
gostatements, they are not needed in that code.Side note: You can’t use a variable in a
usestatement, it has to be the name of a database.