Sqlcmd mode from SSMS - setvar assignment to override variable possible?
I am running this from SSMS 2008R2 with SQLCMD mode enabled.
With the code below:
:SETVAR GREETING "HELLO"
:SETVAR SALUTATION $(GREETING)
SELECT '$(GREETING)'
SELECT '$(SALUTATION)'
I see the following in the results:
HELLO $(GREETING)
I was hoping to see:
HELLO
HELLO
Is there a way to have a script variable while assigning to another script variable? If so, what's the syntax?
0
Jerome
source
to share
2 answers
variable substitution in: SETVAR does not work, because in this case it is just a token substitution.
You can mimic the behavior you expect with the following:
:SETVAR GREETING "HELLO"
DECLARE @salutation VARCHAR(5) = $(GREETING);
SELECT @salutation;
Sincerely.
+2
lucazav
source
to share
You can also do the opposite if you need:
DECLARE @firstVar VARCHAR(30) = 'theUltimateValue';
:setvar secondValue @firstVar
select $(secondValue) as whatIReallyWant
Returns
whatIReallyWant
------------------------------
theUltimateValue
0
JakeMc
source
to share