How to execute sqlcmd from powershell?

前端 未结 7 1488
忘掉有多难
忘掉有多难 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:11

    You could also stop using the external 'SQLCMD.EXE' and use the Invoke-Sqlcmd cmdlet instead:

    Invoke-Sqlcmd is a SQL Server cmdlet that runs scripts that contain statements from the languages (Transact-SQL and XQuery) and commands that are supported by the sqlcmd utility

    Just open the 'sqlps' utility and run

    Invoke-Sqlcmd -InputFile "C:\temp\sql.sql"
    

    Please see Running SQL Server PowerShell

    You can also load the SQL Server snap-ins manually in PowerShell before using 'Invoke-Sqlcmd';
    for MS SQL Server 2012 you can do that by running
    Import-Module SqlPs

    0 讨论(0)
提交回复
热议问题