如何对名称存储在表中的多个数据库执行T-SQL?
我在同一台服务器上有多个数据库(SqlServer 2005),具有相同的架构但不同的数据。
我有一个额外的数据库,其中有一个表存储上述数据库的名称。
因此,我需要做的是迭代这些数据库名称,并实际“切换”到每个数据库名称(使用 [dbname])并执行 T-SQL 脚本。我说清楚了吗?
我举个例子(根据真实情况进行简化):
CREATE TABLE DatabaseNames
(
Id int,
Name varchar(50)
)
INSERT INTO DatabaseNames SELECT 'DatabaseA'
INSERT INTO DatabaseNames SELECT 'DatabaseB'
INSERT INTO DatabaseNames SELECT 'DatabaseC'
假设DatabaseA、DatabaseB、DatabaseC是真实存在的数据库。 假设我需要在这些数据库上创建一个新的 SP。我需要一些脚本来循环这些数据库并执行我指定的 T-SQL 脚本(可能存储在 varchar 变量或其他位置)。
有什么想法吗?
I have several databases (SqlServer 2005) on the same server with the same schema but different data.
I have one extra database which has one table storing the names of the mentioned databases.
So what I need to do is to iterate over those databases name and actually "switch" to each one (use [dbname]) and execute a T-SQL script. Am I clear?
Let me give you an example (simplified from the real one):
CREATE TABLE DatabaseNames
(
Id int,
Name varchar(50)
)
INSERT INTO DatabaseNames SELECT 'DatabaseA'
INSERT INTO DatabaseNames SELECT 'DatabaseB'
INSERT INTO DatabaseNames SELECT 'DatabaseC'
Assume that DatabaseA, DatabaseB and DatabaseC are real existing databases.
So let's say I need to create a new SP on those DBs. I need some script that loops over those databases and executes the T-SQL script I specify (maybe stored on a varchar variable or wherever).
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您应该能够使用 sp_MSforeachdb 无文档存储过程来执行此操作。
You should be able to do this with the sp_MSforeachdb undocumented stored procedure.
此方法要求您将要在每个数据库上执行的 SQL 脚本放在变量中,但应该可行。
另外,正如一些人指出的那样。如果您想要运行一个需要成为批处理中唯一的命令的命令,那么这种方法是有问题的。
对于这种情况,有一个替代方案,但它需要比许多 DBA 可能希望您拥有的权限更多的权限,并且要求您将 SQL 放入单独的文本文件中。
This method requires you to put your SQL script to be executed on each DB in a variable, but should work.
Also, as some have pointed out. This approach is problematic if you want to run a command that needs to be the only thing in a batch.
Here is an alternative for that situation, but it requires more permissions than many DBA's might want you to have and requires you to put your SQL into a separate text file.
使用
USE
命令并重复您的命令。 此处了解如何将 USE 与参数一起使用
Use the
USE
command and repeat your commandsPs. Have a look at how to use USE with a parameter here
我知道这个问题已有 5 年历史了,但我通过 Google 找到了这个问题,所以其他人也可能如此。
我推荐sp_msforeachdb系统存储过程。您不需要创建任何其他存储过程或游标。
鉴于您的数据库名称表已创建:
我这样做是为了总结我从具有相同安装的数据库架构的多个不同站点恢复的许多数据库中的计数。
例子:
-- 在所有站点存档数据库中重复执行 SQL 命令。
最棘手的部分是单引号 '
I know this question is 5 years old, but I found this via Google, so others may as well.
I recommend sp_msforeachdb system stored procedure. You do not need to create any other stored procedures or cursors.
Given your table of database names is already created:
I do this to summarize counts in many databases I have restored from several different sites with the same installed database schema.
Example:
-- Repeat the execution of SQL Commands across all site archived databases.
The trickiest part are the single quotes '
最简单的方法是这样的:
但是,显然它不适用于需要成为批处理中的第一个语句的语句,例如 CREATE PROCEDURE。为此,您可以使用 SQLCLR。创建并部署一个这样的类:
然后你可以这样做
The simplest way is this:
However, obviously it will not work for statements that need to be the first statement in a batch, like CREATE PROCEDURE. For that you can use SQLCLR. Create and deploy a class like this:
Then you can do
我想这在 TSQL 中通常是不可能的,因为正如其他人指出的那样,
您首先需要使用 USE 语句来更改数据库,
后跟要执行的语句,尽管未指定,但该语句是 DDL 语句,必须位于批处理中的第一个语句。
此外,要执行的字符串中不能有 GO。
我找到了一个调用 sqlcmd 的命令行解决方案:
示例代码使用当前的 Windows 登录名从 Master 查询所有活动数据库(替换为您自己的连接并查询数据库),并对找到的每个数据库执行文字 TSQL 命令。 (换行符只是为了清楚起见)
查看 sqlcmd 的 命令行参数。您也可以向它传递一个 TSQL 文件。
如果您希望允许手动选择数据库,请查看 SSMS 工具包。
I guess this will generally not be possible in TSQL, since, as others pointed out,
you first need as USE statement to change the database,
followed by the statement you want to execute, which is, although not specified, a DDL statement which must be first in a batch.
Moreover, you cannot have a GO in a string to be EXECuted.
I found a command-line solution invoking sqlcmd:
Sample code uses current Windows login to query all active databases from Master (replace with your own connection and query for databases), and executes a literal TSQL command on each database thus found. (line breaks for clarity only)
Have a look at the command-line parameters of sqlcmd. You can pass it a TSQL file as well.
If you want to allow manual selection of databases, have a look at SSMS Tools Pack.