Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Concatenate variable values in the third variable in sqlcmd


Concatenate variable values in the third variable in sqlcmd

Author
Message
peacesells
peacesells
SSC Veteran
SSC Veteran (284 reputation)SSC Veteran (284 reputation)SSC Veteran (284 reputation)SSC Veteran (284 reputation)SSC Veteran (284 reputation)SSC Veteran (284 reputation)SSC Veteran (284 reputation)SSC Veteran (284 reputation)

Group: General Forum Members
Points: 284 Visits: 1475
:setvar Instance SUBSTRING(@@SERVERNAME,7,3)
:setvar FolderPath "\\Here\there\where"

I have two sqlcmd variables that i need to concatenate and store the values in the third variable and use that variable in a query.

somthing like
:setvar FullName $(FolderPath)+$(FolderPath)

how can this be done, i tried a couple methods- don't seem to work- any idea?
Eirikur Eiriksson
Eirikur Eiriksson
SSCertifiable
SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)

Group: General Forum Members
Points: 6697 Visits: 17691
peacesells (6/11/2014)
:setvar Instance SUBSTRING(@@SERVERNAME,7,3)
:setvar FolderPath "\\Here\there\where"

I have two sqlcmd variables that i need to concatenate and store the values in the third variable and use that variable in a query.

somthing like
:setvar FullName $(FolderPath)+$(FolderPath)

how can this be done, i tried a couple methods- don't seem to work- any idea?


No need to concatenate the sqlcmd scripting variables, just do the concatenation in the query.
Cool
:setvar PartOne 'first_'
:setvar PartTwo 'second'
go
select char(39) + $(PartOne) + $(PartTwo) + char(39) as full_path
go



Results
full_path
--------------
'first_second'

Eirikur Eiriksson
Eirikur Eiriksson
SSCertifiable
SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)

Group: General Forum Members
Points: 6697 Visits: 17691
peacesells (6/11/2014)
how can this be done, i tried a couple methods- don't seem to work- any idea?


Forgot to answer the question:-D

It's not possible to programmatically set or concatenate the variables without writing to a file.
Cool
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45038 Visits: 39893
Based on what Eirikur posted above, why not build a stored procedure that does this work and execute the stored procedure from SQLCmd, instead?

Also, why are you using SQLCmd? What is it that you're actually trying to do overall? I ask because there may be better methods.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
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