I know my post has a very similar title to other ones in this forum, but I really couldn't find the answer I need.
Here is my problem, I have a SQL Server running on my Windows Server. Inside my SQL Server, I have around 30 databases. All of them have the same tables, and the same stored procedures.
Now, here is the problem, I have this huge script that I need to run in all of these databases. I wish I could do it just once against all my databases.
I tried a couple things like go to "view" >> registered servers >> local server groups >> new server registration. But this solution is for many servers, not many databases.
I know I could do it by typing the database name, but the query is really huge, so it would take too long to run in all databases.
Does anybody have any idea if that is possible?
7条答案
按热度按时间w1e3prcc1#
You can use WHILE loop over all database names and inside loop execute query with EXECUTE . I think that statement
SET @dbname = ...
could be better, but this works too.xcitsw882#
Consider running the script in SQLCMD Mode from SSMS (Query--SQLCMD Mode). This way, you can save the script to a file and run it in the context of each of the desired databases easily:
This technique can also be used to run the script against databases on other servers with the addition of a
:CONNECT
command. The connection reverts back to initial server/database after execution of the entire script:Important gotcha: Note
GO
batch separators are needed for:CONNECT
to work as expected. I recommend includingGO
in the the invoking script like the above example butGO
as the last line in the:r
script file will also provide the desired results. WithoutGO
in this example (or at the end of the script file), the script would run twice on SomeServer and not run against SomeOtherServer at all.w41d8nur3#
ApexSQL Propagate is the tool which can help in this situation. It is used for executing single or multiple scripts on multiple databases, even multiple servers. What you should do is simply select that script, then select all databases against which you want to execute that script:
When you load scripts and databases you should just click the “Execute” button and wait for the results:
tgabmvqs4#
You can write script like this
8e2ybdfx5#
this is the normal way of doing this :
suppose you want to do a select on database DBOther than it would be :
Also check if the table or view is on the dbo schema, if not you should add the schema also : Please notice I use only one dot now after the database name
If any of the databases is on another server on another machine, than make sure the Database is in the Linked Server.
Then you can access the table or view on that database via:
Here is another way that does not requires typing the database name :
Note that this will only work if the tables and fields are exact the same on each database
ffx8fchx6#
You can use the following script to run the same script on a set of databases. Just change the filter in the insert line.
If your SQL Server version does not support table variables, just use Temp Tables but don`t forget to drop them at the end of the script.
gcuhipw97#
Depending on the requirement, you can do this:
Please note that the double dotted notation assumes that the schema is dbo. Otherwise, you need to explicitly write down the schema.
When you need to execute stored procedures on each server, the
@script
variable will have another content.