SQL Server,在多个数据库上执行批处理T-SQL脚本

发布于 2024-08-05 16:27:21 字数 432 浏览 8 评论 0原文

我们的 SQL Server 2000 实例托管着多个相似的数据库,每个客户对应一个数据库。当需要更新它们时,我们使用 Red Gate SQL Compare 在开发数据库和所有客户端数据库的当前状态数据库副本之间生成迁移脚本。

SQL Compare 生成一个事务性脚本,如果一个步骤失败,该脚本将回滚所有内容。但目前我们的系统使用一种方法,将脚本拆分为批处理分隔符(GO 语句),然后单独运行每个命令,这会破坏所有事务性内容。通过编程(在经典 ASP 中)查询数据库时不支持 GO 语句

我想知道如何以编程方式在所有这些数据库(如 250 DB)上运行该脚本(保留事务)或者在工具中手动?在查询分析器中,我们需要选择每个数据库并按运行,这对于我们拥有的数据库数量来说相当长。

Our SQL Server 2000 instance hosts several databases which are all similar, one for each of our client. When comes the time to update them all, we use Red Gate SQL Compare to generate a migration script between the development database and a copy of the current state DB of all the clients database.

SQL Compare generates a script which is transactional, if one step fails, the script rolls back everything. But currently our system uses a method that splits the script on batch separators (the GO statement) and then runs each command separately, which ruins all the transactional stuff. The GO statement is not supported when querying the database by programmation (in classic ASP)

I want to know how I could run that script (keeping the transactions) on all those databases (like 250 DB), programmatically or manually in a tool? In Query Analyzer, we need to select each DB and press Run which is quite long for the number of DB we have.

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

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

发布评论

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

评论(5

下壹個目標 2024-08-12 16:27:21

如果您可以从 SQL 2005 或 2008 使用 SSMS,那么我推荐免费的 SSMS 工具包

If you can use SSMS from SQL 2005 or 2008, then I'd recommend the free SSMS Tool pack

哭了丶谁疼 2024-08-12 16:27:21

我使用外部 sqlcmd 命令行工具。我工作的服务器上也有同样的情况。

我的脚本位于 *.sql 文件中,数据库列表位于第二个文件中。我有一个小的 *.bat 脚本,它遍历所有数据库并使用 sqlcmd 命令执行脚本。

更详细地说,我有这样的:

  • DB.ini 文件,其中包含我想要部署脚本的所有数据库
  • sql/ 目录,我在其中存储所有脚本
  • runIt.bat - 部署脚本的脚本

命令行看起来更像这样:

sqlcmd -S <ComputerName>\<InstanceName> -i <MyScript.sql> -d <database_name> -T

在 SQL Server 2000 中,它是 osql 实用程序

更新

Red Gate 现在有一个名为 的工具SQL Multi Script,它基本上完全符合您的要求。我支持 SQL 2000 到 2008 R2 并在多个数据库上并行运行查询,从而提高性能。

I use external sqlcmd command line tool. I have the same situation on the server I work.

I have the script in *.sql file and the list of databases on the 2nd file. I have small *.bat script which iterate through all the databases and execute script using sqlcmd command.

In more details I have like this:

  • DB.ini file with all the databases on which I want to deploy my script
  • sql/ directory where I store all scripts
  • runIt.bat - script which deploys scripts

The command line looks more-less like this:

sqlcmd -S <ComputerName>\<InstanceName> -i <MyScript.sql> -d <database_name> -T

In SQL Server 2000 it was osql utility

UPDATE

Red Gate now have a tool called SQL Multi Script, which basically does exactly what you want. I supports SQL 2000 to 2008 R2 and running queries on multiple databases in parallel which improve performance.

魔法唧唧 2024-08-12 16:27:21

7 年后,我多次遇到同样的问题,所以我制作并发布了该项目:

TAKODEPLOY

这里有一些功能:

  • 从单个实例获取所有数据库并应用名称过滤器。或者只是一个直接连接。
  • 根据需要混合数据库源。例如,两个直接实例和一个完整实例(带或不带过滤器)。
  • 脚本编辑器(Avalon Text,与 monodevelop 相同)
  • 在执行之前解析脚本并检测错误。
  • 脚本被 GO 语句“分割”。
  • 将部署保存到文件中
  • 在部署之前获取所有数据库的列表。
  • 实时查看正在发生的情况(此处建议使用 PRINT 语句!)。
  • 如果出现错误,自动回滚到独立数据库。
  • 通过 Squirrel 进行透明更新。

您可以在以下位置获取它:https://github.com/andreujuanc/TakoDeploy

7 years later i had the same issue so many times so I made it and published the project:

TAKODEPLOY

Here are some features:

  • Get all databases from a single instance and apply a name filter. Or just a single direct connection.
  • Mix database sources as much as you want. Example, two direct and one full instance with or withut a filter.
  • Script editor (Avalon Text, same monodevelop uses)
  • Scripts are parsed and errors are detected before executing.
  • Scripts are 'splitted' by GO statements.
  • Save your deployment into a file
  • Get a list of all databases before deploying.
  • See in realtime what is happening (PRINT statements are recommended here!).
  • Automatic rollback to independent database if any error occurs.
  • Transparent Updates via Squirrel.

You can get it at: https://github.com/andreujuanc/TakoDeploy

染柒℉ 2024-08-12 16:27:21

不确定这是否有效,但尝试用分号替换 GO 语句,并批量运行整个语句。

Not sure if this will work, but try replacing the GO statements with semicolons, and running the entire statement in one batch.

鸩远一方 2024-08-12 16:27:21

如果我记得的话,您还可以在 SQL Compare 中创建一个脚本,将所有内容更改回其开始时的状态。您可能想要生成这两个脚本。

当我进行这种部署时(已经有一段时间了),我首先加载到一个与 prod 完全相同的临时服务器,然后才开始确保脚本可以在 prod 上运行。如果出现任何失败(通常是由于脚本运行的顺序,例如无法为尚不存在的表设置外键)。我还首先编写了所有表更改的脚本,然后是所有视图更改,然后是所有 UDF 更改,然后是所有存储过程更改。这大大减少了由于对象尚不存在而导致的失败,但我通常仍然有一些需要调整。

If I recall, you can also create a script in SQL Compare to change everything back to the state it started in. You might want to generate both.

When I did this sort of deployment (it's been awhile), I first loaded to a staging server that was made exactly like prod before I started to make sure the scripts would work on prod. If anything failed (usually because of the order that scripts were run, can't set a foreign key to a table that doesn't exist yet for instance). I also scripted al table changes first, then all view changes, then all UDF changes, then all stored proc changes. This cut down greatly onthe failures due to objects not yet existing, but I still usually had a few that needed to be adjusted.

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