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

  • 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
  • Thanks for posting your issue and hopefully someone will answer soon.

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

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

    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)

  • 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

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

    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)

  • 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 5 (of 5 total)

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