简体   繁体   English

从命令行执行 SQL 脚本

[英]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.完毕。

CMD Screenshot after it worked form me CMD 对我起作用后的屏幕截图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用NO脚本文件从命令行执行sql命令? - how to execute sql command from command line using NO script file? MariaDB:从命令行 cmd.exe 创建数据库并执行不带“&lt;”字符的 sql 脚本 - MariaDB : create database and execute sql script without '<' character from the command line cmd.exe 如何通过SQL的批处理脚本命令执行SQL脚本? - How to execute the SQL Script through Batch Script Command from SQL? Cronjob在Perl脚本中不执行命令行 - Cronjob does not execute command line in perl script postgresql-尝试将变量从命令行传递到sql脚本时出错 - postgresql - error trying to pass a variable from command line to a sql script 当我没有 .db 文件时,我可以从 SQLite 命令行执行 .sql 文件吗? - Can I execute .sql file from SQLite command line when I don't have a .db file? 如何连接到Teradata数据库并从命令行执行Teradata SQL查询 - How to connect to Teradata database and execute a Teradata SQL query from Command line 从 python 脚本执行 psql 命令 - Execute psql command from python script 如何从/使用命令行在 SQL Workbench/J 中运行 sql 脚本? - How to run sql script in SQL Workbench/J from/using command line? SQL将EXECUTE命令中的值存储到变量中 - SQL Store a value from an EXECUTE Command into a variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM