I need to access variables in whole script that is divided to few sections by GO command. How to do that?
Below is source example (that doesn’t work):
--script info (begining of script file)
DECLARE @ScriptCode NVARCHAR (20) = '20120330-01'
--some queries
GO
--and there I cannot use @ScriptCode variable
INSERT INTO DBScriptsHistory(ScriptCode) VALUES(@ScriptCode)
That’s correct, because variables exist only within the current batch. Assuming that you need to use the GO statement (e.g. CREATE VIEW must be the first statement in a batch), the simplest solution is probably to use sqlcmd scripting variables.