How to execute sqlcmd from powershell?

前端 未结 7 1491
忘掉有多难
忘掉有多难 2020-12-29 05:11

I have a string in powershell, which contains a native sqlcmd command. The command itself can be executed successfully in cmd.exe. I have difficulty in executing them in pow

7条回答
  •  一生所求
    2020-12-29 06:08

    This is what worked for me for using sqlcmd from within the powershell script using the & operator, see sample to generate a csv file:

    & cmd /c "sqlcmd -S $sv -i $PROCFILE -s, -v varDB = $dbclean -o $filename"

    $sv has server name like SERVERNAME\INSTANCE

    $PROCFILE is like d:\TSTSQL\Sqlquery.SQL

    $filename is d:\TSTSQL\Desiredoutfilename.CSV

    $dbclean is a parameter passed to the sql file

提交回复
热议问题