1

Can someone tell me how to swap this absolute path with a relative one for my sqlcmd upgrade script?

-- how can I get rid of the following absolute path variable from my script?
:SETVAR ScriptDir "C:\absolutePath\"  

:SETVAR DeployLog "upgradeDb.out.log"
:SETVAR ErrorLog "upgradeDb.err.log"
:Error $(ScriptDir)$(ErrorLog)
:OUT $(ScriptDir)$(DeployLog)

:ON ERROR EXIT
print ' run scripts a and b'
:r $(ScriptDir)"a.sql"
:r $(ScriptDir)"b.sql"

PRINT 'finished'
GO

:EXIT 
print 'problem...'
GO

Originally posted (in the wrong place) here: https://softwareengineering.stackexchange.com/questions/318033/how-to-use-a-relative-path-in-sqlcmd-script

3
  • It's not clear what your question is. Are you asking how to edit the script file? Do you not know what the path is? Something else?
    – mustaccio
    Commented May 11, 2016 at 14:28
  • 1
    How are you executing this script? Command line via SQLCMD.EXE or in SSMS using SQLCMD mode? Commented May 11, 2016 at 18:15
  • 1
    the script's being run in SSMS in sqlcmd mode. If I don't set the path explicitly the cwd turns out to be the folder that SSMS is in IIRC. These scripts go out to a customer and then I don't know where they're being run from and I'm having to tell the customer to manually edit them before running them in SSMS. It seems like the kind of thing I shouldn't have to be doing. Thanks,
    – timB33
    Commented Jun 29, 2016 at 9:47

1 Answer 1

1

If you want a relative path, just use relative path notation.
:SETVAR ScriptDir "../../"

That will set your ScriptDir to two directories higher than wherever you invoked sqlcmd from.

or :SETVAR ScriptDir "moo/oink/bah/"

That will look for scripts in the moo/oink/bah subdirectory of wherever you invoked sqlcmd from.

2
  • 2
    I've tried this and although you'd think it would work it doesn't. CWD turns out to be location of SSMS.exe and not the master script's.
    – timB33
    Commented Jun 29, 2016 at 9:48
  • I usually launch sqlcmd.exe from PowerShell, in which case the relative paths work fine.
    – dpw
    Commented Jun 29, 2016 at 16:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.