SQL Clone
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
Old Hand
Old Hand (342 reputation)Old Hand (342 reputation)Old Hand (342 reputation)Old Hand (342 reputation)Old Hand (342 reputation)Old Hand (342 reputation)Old Hand (342 reputation)Old Hand (342 reputation)

Group: General Forum Members
Points: 342 Visits: 1481
: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
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15378 Visits: 18614
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
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15378 Visits: 18614
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 Guru
SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)

Group: General Forum Members
Points: 88056 Visits: 41128
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

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