如何使用powershell执行.sql文件? [英] How to execute .sql file using powershell?

查看:55
本文介绍了如何使用powershell执行.sql文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 .sql 文件.我正在尝试通过 Powershell 脚本传递连接字符串详细信息并调用 .sql 文件.

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

我正在搜索并想出了一个与 Invoke-sqlcmd 相关的 cmdlet.当我试图找到一个对应于 SQL 的模块时,我在我的机器上没有找到任何一个.

I was searching and came up with a cmdlet related to Invoke-sqlcmd. While I was trying to find a module corresponding to SQL, I did not find any one in my machine.

我应该在我的机器上安装任何东西(机器已经有 SQL Server Management Studio 2008 R2)来获取模块,还是有什么简单的方法可以使用 Powershell 执行 .sql 文件?

Should I install anything in my machine (the machine already has SQL Server Management Studio 2008 R2) to get the modules or is there any easy way to execute .sql files using Powershell?

推荐答案

尝试查看是否存在 SQL 管理单元:

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

如果是这样

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.

这篇关于如何使用powershell执行.sql文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆