Error: “Must declare the scalar variable” for insert statements in multiple database

后端 未结 3 2054
梦如初夏
梦如初夏 2021-02-07 11:34

I created a SQL script to add an entry in different database. However, when I run the script through SQL Server Management Studio.

declare @address varchar(50)
s         


        
3条回答
  •  佛祖请我去吃肉
    2021-02-07 12:00

    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
    

提交回复
热议问题