如何使用批处理文件运行多个SQL脚本?

发布于 2024-12-26 11:51:52 字数 550 浏览 4 评论 0原文

我有一个案例,我有 10 多个 SQL 脚本

我不想一一运行我的所有脚本。

有没有一种方法可以让我在 SQL Management studio 中连续运行我的所有脚本。

我找到了这篇帖子。创建批处理文件似乎更容易。

这就是您所需要的:

@echo off
ECHO %USERNAME% started the batch process at %TIME%  >output.txt


for %%f in (*.sql) do (
 (
sqlcmd.exe  -S servername -E   -d databasename -i %%f >>output.txt
)


pause

替换服务器名和数据库名,但它似乎不起作用。

有什么想法吗?

I have a case where i have got 10+ SQL script.

I don't want to go and run all my scripts 1 by 1.

Is there a way that i can run all my scripts in succession in SQL Management studio.

I found this post. Creating a batch file seems easier.

This is all you need:

@echo off
ECHO %USERNAME% started the batch process at %TIME%  >output.txt


for %%f in (*.sql) do (
 (
sqlcmd.exe  -S servername -E   -d databasename -i %%f >>output.txt
)


pause

Replacing servername and databasename, but it seems to be not working.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

絕版丫頭 2025-01-02 11:51:52

你有一个不匹配的括号。
尝试

for %%f in (*.sql) do sqlcmd.exe -S servername -E -d databasename -i %%f >>output.txt

我刚刚将其保存在 .cmd 文件中,它似乎正在工作。

You've got an unmatched parenthesis, there.
Try

for %%f in (*.sql) do sqlcmd.exe -S servername -E -d databasename -i %%f >>output.txt

I just saved it in a .cmd file and it appears to be working.

浸婚纱 2025-01-02 11:51:52

是的,这是可能的。您可以使用 SQLCMD 的 :r 命令来完成此操作。

我强烈建议您阅读本文并使用 SQLCMD

http://www.mssqltips.com/sqlservertip/1543/using-sqlcmd-to-execute-multiple-sql-server-scripts/

Yes, it's possible. You can do it with :r command of SQLCMD.

I strongly recommend you to read this article and do it with SQLCMD

http://www.mssqltips.com/sqlservertip/1543/using-sqlcmd-to-execute-multiple-sql-server-scripts/

孤独患者 2025-01-02 11:51:52

您可以创建一个存储过程来调用所有脚本。您还可以创建一个计划来自动运行脚本。

http://msdn.microsoft.com/en -us/library/aa174792(v=sql.80).aspx

You can create a Strored Procedure to call all your Scripts. You could also create a schedule plan to run the scripts automaticaly.

http://msdn.microsoft.com/en-us/library/aa174792(v=sql.80).aspx

余罪 2025-01-02 11:51:52

这是一个带有源代码的开源实用程序 http://scriptzrunner.codeplex.com/

该实用程序是用以下语言编写的c# 并允许您拖放许多 sql 文件并开始针对数据库运行它们。

Here is an open source utility with source code http://scriptzrunner.codeplex.com/

This utility was written in c# and allows you to drag and drop many sql files and start running them against a database.

烟柳画桥 2025-01-02 11:51:52

您可以使用 SMSS 的 批处理编译器 插件,它可以让您一次运行多个脚本、创建 SQLCMD 脚本或将它们合并到*.sql 文件。

You can use Batch Compiler add-in for SMSS, it let's you run multiple scripts at once, create SQLCMD scripts or consolidate them into a *.sql file.

哎呦我呸! 2025-01-02 11:51:52

一些批量技巧

cd %~dp0 //use this if you use 'for xxx in', it solved most of my problems 

ECHO %USERNAME% started the batch process at %TIME%  >output.txt


for %%f in (*.sql) do (
(
sqlcmd.exe  -S servername -E -d databasename -i %%f >>output.txt
)
echo %errorlevel%
pause

Some batch trick

cd %~dp0 //use this if you use 'for xxx in', it solved most of my problems 

ECHO %USERNAME% started the batch process at %TIME%  >output.txt


for %%f in (*.sql) do (
(
sqlcmd.exe  -S servername -E -d databasename -i %%f >>output.txt
)
echo %errorlevel%
pause
柏林苍穹下 2025-01-02 11:51:52

如果您想通过批处理程序运行 Oracle SQL 文件,那么下面的代码将会很有用。只需复制&更改数据库凭据和数据库名称

@echo off
for %%i in ("%~dp0"*.sql) do echo @"%%~fi" >> "%~dp0all.sql"

echo exit | sqlplus scott/tiger@orcl @"c:\users\all.sql"
pause

基本上,您需要将此批处理文件放在包含所有 SQL 文件的文件夹中。它首先会获取目录中的所有sql文件名,并使用sql文件名加载它们的完整路径。然后,它将写入文件all.sql,然后sqlplus将调用该all.sql来执行该目录中的所有sql文件。

If you want to run Oracle SQL files through a Batch program, then the code below will be useful. Just copy & change the Database credential and DB names

@echo off
for %%i in ("%~dp0"*.sql) do echo @"%%~fi" >> "%~dp0all.sql"

echo exit | sqlplus scott/tiger@orcl @"c:\users\all.sql"
pause

Basically, you need to put this batch file in the folder where you have all the SQL files. It will first get all the sql file names in the directory and load their full path with the sql file names. Then, it will write into a file all.sql and then sqlplus will call that all.sql to execute all the sql files that you have in that directory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文