sqlcmd - Unable to pass in exact file path as an input variable to :out

  • Weegee2017

    SSC Eights!

    Points: 942

    Hi there

    I have a routine which outputs an ID generated from a stored procedure into a text file as follows:

    :!!if exist \\LONDEVSQL01\Staging\SEG_DEV_H01\Import\SupplementaryImport\StoredProcedureRunTrackerID.txt  del \\LONDEVSQL01\Staging\SEG_DEV_H01\Import\SupplementaryImport\StoredProcedureRunTrackerID.txt

    --:connect localhost\SQL2008R2

    :out \\LONDEVSQL01\Staging\SEG_DEV_H01\Import\SupplementaryImport\StoredProcedureRunTrackerID.txt

    select Ltrim(Rtrim(@StoredProcedureRunTrackerID))

    Now i wanted to change this , so that the path for :out is not hardcoded and passed in as a variable, ie

    :on error exit

    :setvar DirectoryRootPath "\\LONDEVSQL01\Staging\SEG_DEV_H01\Import\SupplementaryImport\StoredProcedureRunTrackerID.txt"

    then calling

    :!!if exist  @DirectoryRootPath del  '$(DirectoryRootPath)'

    --:connect localhost\SQL2008R2

    :out '$(DirectoryRootPath)'

    select @StoredProcedureRunTrackerID

    But this not produce my output file

    Is there something syntatically that I am doing wrong here?

    The SP is executing ok, just the output of the ID into a field isnt

    Hi there 

    I have a routine which outputs an ID generated from a stored procedure into a text file as follows:

    :!!if exist \\LONDEVSQL01\Staging\SEG_DEV_H01\Import\SupplementaryImport\StoredProcedureRunTrackerID.txt del \\LONDEVSQL01\Staging\SEG_DEV_H01\Import\SupplementaryImport\StoredProcedureRunTrackerID.txt
    --:connect localhost\SQL2008R2
    :out \\LONDEVSQL01\Staging\SEG_DEV_H01\Import\SupplementaryImport\StoredProcedureRunTrackerID.txt
    select Ltrim(Rtrim(@StoredProcedureRunTrackerID))


    Now i wanted to change this , so that the path for :out is not hardcoded and passed in as a variable, ie


    :on error exit
    :setvar DirectoryRootPath "\\LONDEVSQL01\Staging\SEG_DEV_H01\Import\SupplementaryImport\StoredProcedureRunTrackerID.txt"

    then calling


    :!!if exist @DirectoryRootPath del '$(DirectoryRootPath)'
    --:connect localhost\SQL2008R2
    :out '$(DirectoryRootPath)'
    select @StoredProcedureRunTrackerID

    But this not produce my output file

    Is there something syntatically that I am doing wrong here?

    The SP is executing ok, just the output of the ID into a field isnt
  • Site Owners

    SSC Guru

    Points: 80379

    Thanks for posting your issue and hopefully someone will answer soon.

    This is an automated bump to increase visibility of your question.

  • Jeff Moden

    SSC Guru

    Points: 995161

    I've always been frustrated by things like that.  It's a complete tease that MS built into the system.  My answer has always been to use xp_CmdShell for such things... and, no... it's not the security risk that most will make it out to be if you set it up and use it correctly.  Having xp_CmdShell turned off will not protect you from anything.  If someone get's in with sysadmin privs, they can turn it on.  If they can't, they can't use it even if it is turned on.  And, except for DBAs that have sysadmin privs, you can allow users to execute stored procedures that use it without give those same users privs to use xp_CmdShell directly.

    Of course, I stopped using the SQLCmd mode a very long time ago because it couldn't do things like you describe but that may have changed.  I'd love for someone to blast an answer for this.

    --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
    "Change is inevitable... change for the better is not."
    When you put the right degree of spin on it, the number 3|8 is also a glyph that describes the nature of a DBAs job. 😉

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

  • Steve Jones - SSC Editor

    SSC Guru

    Points: 716659

    OK, I played around with this. I think your quotes are the problem. This works for me:

     

    :setvar DirectoryRootPath "C:\Users\Steve\OneDrive\Documents\SQL\StoredProcedureRunTrackerID.txt"

    --:!!if exist C:\Users\Steve\OneDrive\Documents\SQL\StoredProcedureRunTrackerID.txt del C:\Users\Steve\OneDrive\Documents\SQL\StoredProcedureRunTrackerID.txt
    !!if exist $(DirectoryRootPath) del $(DirectoryRootPath)

    :connect Plato\SQL2017

    :out $(DirectoryRootPath)

    select * FROM dbo.MyTable

    This deletes the text file and puts the data out there

  • Jeff Moden

    SSC Guru

    Points: 995161

    Steve,

    Will that actually work in a stored procedure?  I'm thinking "not" and that it could only work from a "routine" (if you understand the difference) but I could be wrong.

    --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
    "Change is inevitable... change for the better is not."
    When you put the right degree of spin on it, the number 3|8 is also a glyph that describes the nature of a DBAs job. 😉

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

  • Steve Jones - SSC Editor

    SSC Guru

    Points: 716659

    Should not, as a proc is not in SQLCMD mode. What is happening here in a script is SSMS in interpreting this a certain way.

    You can call a proc here, but SQLCMD mode is an interactive execution (or using sqlcmd.exe)

Viewing 6 posts - 1 through 6 (of 6 total)

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