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

Concatenate variable values in the third variable in sqlcmd Expand / Collapse
Author
Message
Posted Wednesday, June 11, 2014 4:29 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 8:46 AM
Points: 225, Visits: 1,005
: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?
Post #1579844
Posted Sunday, June 15, 2014 4:16 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 9:30 AM
Points: 1,213, Visits: 3,542
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.

:setvar PartOne 'first_'
:setvar PartTwo 'second'
go
select char(39) + $(PartOne) + $(PartTwo) + char(39) as full_path
go

Results
full_path
--------------
'first_second'
Post #1580920
Posted Sunday, June 15, 2014 4:42 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 9:30 AM
Points: 1,213, Visits: 3,542
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

It's not possible to programmatically set or concatenate the variables without writing to a file.

Post #1580922
Posted Sunday, June 15, 2014 9:35 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 11:30 PM
Points: 36,706, Visits: 31,156
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1580939
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse