Trying to tidy up my SQL script with SQLCMD mode and I ran into an issue:
:setvar db_suffix "some_suffix"
:setvar some_db "some_db_$(db_suffix)"
print 'some_db: $(some_db)'
The output of that is:
some_db: some_db_$(db_suffix)
However, what I expected was:
some_db: some_db_some_suffix
Is there a way to have variable interpolation like this?
(Note that T-SQL functions such as CONCAT()
won't work since I'll be using the variable as a database name).
Best Answer
Kinda sorta, in a way. Just not directly.
You need to keep in mind a few things:
The value that you set a variable to via
:setvar
is a simple, literal value. You can see this by running just the following two lines:which returns:
If SQLCMD / SQLCMD-mode attempted to parse the value portion of
:setvar
in any way, then it would error on$(var2)
not being defined.Variable substitution is allowed in some other SQLCMD commands.
This means that you can, for example, execute a shell command while passing a SQLCMD variable to that command line:
The two lines above will create / overwrite a file, C:\TEMP\setvar.txt, with the text of ":setvar other_db [MyDB_$(db_suffix)]" where
$(db_suffix)
is replaced with its value of "some_suffix".SQLCMD commands in a file / script that is imported via the
:r
command are processed in a second-pass, allowing for variables set in those external files to be reflected in the main script:Returns:
SQLCMD commands are interpreted per-each batch !! So if you want to change the concatenated value, then you need to separate each creating and reading of the temp file, else the value you get will be the last one to be set since all of the
!!
and:r
commands will be processed before the variable values are substituted in, and then the batch can be submitted to SQL Server to process the T-SQL. For example:Will return:
BUT, uncomment the
--GO
and you will get the following: