SQLCMD mode and T-SQL variables

  • 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

  • This was removed by the editor as SPAM

  • Did you find the solution?

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply