如何从批处理文件中运行.sql文件?

时间:2021-06-23 02:04:08

I am running sql server 2008 express and i need to schedule some stored procedures to run nightly...so i have built out these .sql files which i would want to run from .bat file...i need to know the command to execute these .sql files one by one and store their results i guess...can anyone help me out?

我正在运行sql server 2008 express,我需要安排一些存储过程每晚运行...所以我已经建立了这些.sql文件,我想从.bat文件运行...我需要知道命令到一个接一个地执行这些.sql文件并存储他们的结果我猜...任何人都可以帮助我吗?

5 个解决方案

#1


15  

I answered this in this other question:

我在另一个问题中回答了这个问题:

You should invoke the sqlcmd command-line tool from your batch file. Assuming your sql file is "backup.sql", the command line would be something like:

您应该从批处理文件中调用sqlcmd命令行工具。假设您的sql文件是“backup.sql”,命令行将类似于:

sqlcmd -E -S yoursqlinstance -i backup.sql

-E uses trusted connection, replace with -U and -P if you need to specify a SQL username and password. See also this article with examples.

-E使用可信连接,如果需要指定SQL用户名和密码,请使用-U和-P替换。另见本文的示例。

#2


3  

See the sqlcmd utility:

请参阅sqlcmd实用程序:

http://msdn.microsoft.com/en-us/library/ms165702.aspx

http://msdn.microsoft.com/en-us/library/ms165702.aspx

This allows you to run sql scripts from the command line

这允许您从命令行运行sql脚本

#3


1  

osql:

OSQL:

http://www.di-mgt.com.au/osqlUtility.htm

http://www.di-mgt.com.au/osqlUtility.htm

#4


1  

I don't use SQL Server, but a batch file is just a list of DOS commands. So whatever you use to execute SQL files from the commandline can be used in a batch file.

我不使用SQL Server,但批处理文件只是DOS命令的列表。因此,无论您使用什么来执行命令行中的SQL文件都可以在批处理文件中使用。

A quick google search turns up:

快速谷歌搜索出现了:

sqlcmd -i <inputfile> -o <outputfile>

#5


0  

Hope this helps you :

希望这可以帮助你:

sqlplus UserName/Password@DataBase @C:\sqlFolder\sqlFile.sql

P.S : Don't forget to add the command "commit;" at the end of sql file (sqlFile.sql), this command order Oracle to save performed changes in database

P.S:别忘了添加命令“commit;”在sql文件末尾(sqlFile.sql),此命令命令Oracle保存在数据库中执行的更改

#1


15  

I answered this in this other question:

我在另一个问题中回答了这个问题:

You should invoke the sqlcmd command-line tool from your batch file. Assuming your sql file is "backup.sql", the command line would be something like:

您应该从批处理文件中调用sqlcmd命令行工具。假设您的sql文件是“backup.sql”,命令行将类似于:

sqlcmd -E -S yoursqlinstance -i backup.sql

-E uses trusted connection, replace with -U and -P if you need to specify a SQL username and password. See also this article with examples.

-E使用可信连接,如果需要指定SQL用户名和密码,请使用-U和-P替换。另见本文的示例。

#2


3  

See the sqlcmd utility:

请参阅sqlcmd实用程序:

http://msdn.microsoft.com/en-us/library/ms165702.aspx

http://msdn.microsoft.com/en-us/library/ms165702.aspx

This allows you to run sql scripts from the command line

这允许您从命令行运行sql脚本

#3


1  

osql:

OSQL:

http://www.di-mgt.com.au/osqlUtility.htm

http://www.di-mgt.com.au/osqlUtility.htm

#4


1  

I don't use SQL Server, but a batch file is just a list of DOS commands. So whatever you use to execute SQL files from the commandline can be used in a batch file.

我不使用SQL Server,但批处理文件只是DOS命令的列表。因此,无论您使用什么来执行命令行中的SQL文件都可以在批处理文件中使用。

A quick google search turns up:

快速谷歌搜索出现了:

sqlcmd -i <inputfile> -o <outputfile>

#5


0  

Hope this helps you :

希望这可以帮助你:

sqlplus UserName/Password@DataBase @C:\sqlFolder\sqlFile.sql

P.S : Don't forget to add the command "commit;" at the end of sql file (sqlFile.sql), this command order Oracle to save performed changes in database

P.S:别忘了添加命令“commit;”在sql文件末尾(sqlFile.sql),此命令命令Oracle保存在数据库中执行的更改