Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

SQLCMD variable scripting - injecting a variable into another Expand / Collapse
Author
Message
Posted Wednesday, January 19, 2011 5:14 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, September 9, 2013 6:28 AM
Points: 37, 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]
Post #1049945
Posted Monday, January 24, 2011 11:55 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 5:14 PM
Points: 7,125, Visits: 12,719
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
Post #1052612
Posted Tuesday, January 25, 2011 3:25 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, September 9, 2013 6:28 AM
Points: 37, 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.
Post #1052974
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse