I'm curious if it is possible to pass a T-SQL variable to a SQLCMD variable from SSMS. In the code below, I would like the connect statement to connect to "SRV\INSTANCE". The result of the code is SQLCMD taking the variable name "@servername" literally.
What I'm trying to accomplish is reading a list of servers out of a table so my scripts can be run in multiple environments without hardcoding servernames.
declare
@servername sysname
select @servername = 'SRV\INSTANCE', @count = 0
--Pass T-SQL "@servername" variable to SQLCMD "SERVERNAME" variable
:setvar SERVERNAME @servername
:connect $(SERVERNAME)
select @@servername
GO