Concatenate variable values in the third variable in sqlcmd

  • :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?

  • 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'

  • 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.

    😎

  • 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.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply