How to execute .sql file using powershell?

后端 未结 5 545
一生所求
一生所求 2020-11-28 04:22

I have a .sql file. I am trying to pass connection string details through a Powershell script and invoke an .sql file.

I was searching and

5条回答
  •  无人及你
    2020-11-28 05:27

    Try to see if SQL snap-ins are present:

    get-pssnapin -Registered
    
    Name        : SqlServerCmdletSnapin100
    PSVersion   : 2.0
    Description : This is a PowerShell snap-in that includes various SQL Server cmdlets.
    
    Name        : SqlServerProviderSnapin100
    PSVersion   : 2.0
    Description : SQL Server Provider
    

    If so

    Add-PSSnapin SqlServerCmdletSnapin100 # here lives Invoke-SqlCmd
    Add-PSSnapin SqlServerProviderSnapin100
    

    then you can do something like this:

    invoke-sqlcmd -inputfile "c:\mysqlfile.sql" -serverinstance "servername\serverinstance" -database "mydatabase" # the parameter -database can be omitted based on what your sql script does.
    

提交回复
热议问题