SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQLCMD variable scripting - injecting a variable into another


SQLCMD variable scripting - injecting a variable into another

Author
Message
d_sysuk
d_sysuk
SSC-Enthusiastic
SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)

Group: General Forum Members
Points: 169 Visits: 227
Okay this should be possible; if the injection is working correctly. What I am trying to do, is have a sqlcmd variable inject its value into another sqlcmd variable. Any ideas what I'm doing wrong ?
(Hint copy into SSMS, and select Query -> SQLCMD mode to run this)
:SETVAR DB_NAME_ASSIGN "TEST TEXT"
:SETVAR DB_NAME_TEXT "EXPECT INSERT OF DB_NAME_ASSIGN HERE: $(DB_NAME_ASSIGN)"
/*Get output*/
SELECT '$(DB_NAME_TEXT)' AS OUTPUT
SELECT '$(DB_NAME_ASSIGN)' AS OUTPUT

/*Expect DB_NAME_TEXT OUTPUT to be:*/
/* DB_NAME_TEXT "EXPECT INSERT OF DB_NAME_ASSIGN HERE: TEST TEXT*/
/*Unfortunatly I get : EXPECT INSERT OF DB_NAME_ASSIGN HERE: $(DB_NAME_ASSIGN)*/


Any ideas what I can do to get this to work (as I kind of need nested variable injection - nothing in BOL saying its not supported, possibly a bug)?

[SQL 2008 SP1 Dev Edition]
Orlando Colamatteo
Orlando Colamatteo
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: General Forum Members
Points: 40342 Visits: 14413
I spent a few minutes looking around and experimenting and I did not see a way to do it. I don't know everything you're trying to do, however while it may not be your preference this technique might work for you:

:SETVAR DB_NAME_ASSIGN "TEST TEXT"
:SETVAR DB_NAME_TEXT "EXPECT INSERT OF DB_NAME_ASSIGN HERE: "
/*Get output*/
SELECT '$(DB_NAME_TEXT)' + '$(DB_NAME_ASSIGN)' AS OUTPUT

/* Delivers:

EXPECT INSERT OF DB_NAME_ASSIGN HERE: TEST TEXT

*/



__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
d_sysuk
d_sysuk
SSC-Enthusiastic
SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)

Group: General Forum Members
Points: 169 Visits: 227
Yes, had to take the approach of breaking it down into further variables, then concatting the results together.

Seems like it would be a nice thing to have the ability to inject sqlcmd var values into another one.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search