从命令行执行SQL脚本。

时间:2022-08-29 01:14:17

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?

蝙蝠档案会怎样?

How can I specify in the script the password and that I use in SQL Express?

如何在脚本中指定我在SQL Express中使用的密码?

3 个解决方案

#1


46  

Take a look at the sqlcmd utility. It allows you to execute SQL from the command line.

看一下sqlcmd实用程序。它允许您从命令行执行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"

#2


34  

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 /? to get all the options you can use with sqlcmd utility

从命令提示符运行sqlcmd /?要获取sqlcmd实用工具可以使用的所有选项

#3


18  

If you use Integrated Security, you might want to know that you simply need to use -E like this:

如果您使用集成安全性,您可能想知道您只需要这样使用-E:

sqlcmd -S Serverinstance -E -i import_file.sql

#1


46  

Take a look at the sqlcmd utility. It allows you to execute SQL from the command line.

看一下sqlcmd实用程序。它允许您从命令行执行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"

#2


34  

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 /? to get all the options you can use with sqlcmd utility

从命令提示符运行sqlcmd /?要获取sqlcmd实用工具可以使用的所有选项

#3


18  

If you use Integrated Security, you might want to know that you simply need to use -E like this:

如果您使用集成安全性,您可能想知道您只需要这样使用-E:

sqlcmd -S Serverinstance -E -i import_file.sql