passing path to SqlCmd within powershell script

后端 未结 4 2108
面向向阳花
面向向阳花 2021-02-13 09:28

I\'m trying to write a powershell script which will execute sqlcmd.exe to run a sql script. The script contains a SQLCMD variable which I want to pass in on the command line via

4条回答
  •  甜味超标
    2021-02-13 10:07

    finally worked it out. for the next sucker to try this here is the solution

    powershell script looks like

    $myPath = "`"C:\Path`"" 
    sqlcmd.exe -SmySQLInstance -i./test.sql -v myvar=$myPath
    

    my test.sql file can then use the variable like this

    PRINT "$(myvar)"
    

    the key here is understanding how powershell does escape characters. More info on that here

提交回复
热议问题