Error: "Must declare the scalar variable" for insert statements in multiple database
the variable @address only lives in the batch that its defined in, batches are delimited by the the go statement, where it goes out of scope.
try this:
declare @address varchar(50)
set @address = 'Hope'
insert into DB1.dbo.Address
values (@address)
insert into DB2.dbo.Address
values (@address)
go
It's the GO statement.
all local variable declarations must be grouped in a single batch. This is done by not having a GO command until after the last statement that references the variable. (from MSDN)
It's because you are using go
between the statement that declares the variable and the statement that uses it.
The go
command is not an SQL command, it's a separator between sessions in Management Studio. Just remove all the go
commands in your query, and you can use the variable all the way.