[英]Execute SQL script from command line
I need to alter a database using a batch file, for a simple example, drop a table.我需要使用批处理文件更改数据库,举个简单的例子,删除一个表。 I´m using local SQL Express (SQL Server 2008 R2) with user
sa
and its password.我正在使用本地 SQL Express (SQL Server 2008 R2) 和用户
sa
及其密码。
How would the bat file be? bat文件会是怎样的?
How can I specify in the script the password and that I use in SQL Express?如何在脚本中指定密码以及我在 SQL Express 中使用的密码?
Take a look at the sqlcmd
utility.查看
sqlcmd
实用程序。 It allows you to execute SQL from the command line.它允许您从命令行执行 SQL。
http://msdn.microsoft.com/en-us/library/ms162773.aspx http://msdn.microsoft.com/en-us/library/ms162773.aspx
It's all in there in the documentation, but the syntax should look something like this:这一切都在文档中,但语法应如下所示:
sqlcmd -U myLogin -P myPassword -S MyServerName -d MyDatabaseName
-Q "DROP TABLE MyTable"
You can do like this你可以这样做
sqlcmd -S <server Name> -U sa -P sapassword -i inputquery_file_name -o outputfile_name
From your command prompt run sqlcmd /?
从命令提示符运行
sqlcmd /?
to get all the options you can use with sqlcmd
utility获取可以与
sqlcmd
实用程序一起使用的所有选项
If you use Integrated Security , you might want to know that you simply need to use -E
like this:如果您使用Integrated Security ,您可能想知道您只需要像这样使用
-E
:
sqlcmd -S Serverinstance -E -i import_file.sql
If you want to run the script file then use below in cmd如果要运行脚本文件,请在 cmd 中使用
sqlcmd -U user -P pass -S servername -d databasename -i "G:\Hiren\Lab_Prodution.sql"
Feedback Guys, first create database example live;反馈 伙计们,首先现场创建数据库示例; before execute sql file below.
在执行下面的 sql 文件之前。
sqlcmd -U SA -P yourPassword -S YourHost -d live -i live.sql
First set the path of MYSQL in Environment variable-> System variables-> Click on Path-> Add -> "C:\Program Files\MySQL\MySQL Server 8.0\bin"首先在环境变量->系统变量->点击路径->添加->“C:\Program Files\MySQL\MySQL Server 8.0\bin”中设置MYSQL的路径
Open cmd-> navigate to the folder which has the sql script-> type as below -> mysql --user=root -p < employees.sql
-> Enter the password which was set during MYSQL setup-> hit enter Open cmd-> navigate to the folder which has the sql script-> type as below ->
mysql --user=root -p < employees.sql
-> Enter the password which was set during MYSQL setup-> hit enter
Done.完毕。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.