执行设置使用批处理文件的SQL查询? [英] Executing set of SQL queries using batch file?

查看:189
本文介绍了执行设置使用批处理文件的SQL查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是SQL Server数据库。我有这些SQL查询:

I am using a SQL Server database. I have these SQL queries:

Delete from TableA;
Delete from TableB;
Delete from TableC;
Delete from TableD;
Delete from TableE;

是否可以运行使用批处理文件,这些脚本?该数据库是一个远程数据库。

Is it possible to run these scripts using a batch file? The database is a remote database.

谢谢!

推荐答案

保存命令在 .SQL 文件,例如: ClearTables.sql ,说在你的 C:\\ TEMP 文件夹

Save the commands in a .SQL file, ex: ClearTables.sql, say in your C:\temp folder.

目录 C:\\ TEMP \\ ClearTables.sql

Delete from TableA;
Delete from TableB;
Delete from TableC;
Delete from TableD;
Delete from TableE;

然后使用 SQLCMD 如下执行它。既然你说的数据库是远程的,请使用以下语法(更新为您的服务器和数据库实例名称后)。

Then use sqlcmd to execute it as follows. Since you said the database is remote, use the following syntax (after updating for your server and database instance name).

sqlcmd -S <ComputerName>\<InstanceName> -i C:\Temp\ClearTables.sql

例如,如果您的远程计算机的名称是SQLSVRBOSTON1和数据库实例名称是MYDB1,则该命令会。

For example, if your remote computer name is SQLSVRBOSTON1 and Database instance name is MyDB1, then the command would be.

sqlcmd -E -S SQLSVRBOSTON1\MyDB1 -i C:\Temp\ClearTables.sql

另外请注意, -E 指定默认的认证。如果你有一个用户名和密码进行连接,使用 -U -P 开关。

Also note that -E specifies default authentication. If you have a user name and password to connect, use -U and -P switches.

您将通过打开 CMD 命令窗口中执行这一切。

You will execute all this by opening a CMD command window.

使用批处理文件。

如果您想将其保存在批处理文件,然后双击运行它,做如下:

If you want to save it in a batch file and double-click to run it, do it as follows.

创建,并保存 ClearTables.bat 像这样

echo off
sqlcmd -E -S SQLSVRBOSTON1\MyDB1 -i C:\Temp\ClearTables.sql
set /p delExit=Press the ENTER key to exit...:

然后双击运行它。它会执行的命令,等到你preSS一键退出,这样你就可以看到命令的输出。

Then double-click it to run it. It will execute the commands and wait until you press a key to exit, so you can see the command output.

这篇关于执行设置使用批处理文件的SQL查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆