how to run same query on multiple databases in sql server

Devendra Kumar Sahu 246 Reputation points
2022-10-27T07:10:53.277+00:00

I have Single Instant and around 15-20 Database i need to run this below query for each database
currently i use this query

use [A]
Delete from DeviceCommands
use [B]
Delete from DeviceCommands
use [C]
Delete from DeviceCommands
use [D]
Delete from DeviceCommands
.
.
.
.
.
.
use [O]
Delete from DeviceCommands

This process is too long time

But i want to run "Delete from DeviceCommands" this command run single query on multiple databases

SQL Server Other
{count} vote

5 answers

Sort by: Most helpful
  1. Bjoern Peters 8,921 Reputation points
    2022-10-27T11:21:52.637+00:00

    The easiest way to achieve this is what Viorel-1 stated above -

    Viorel-1 · 3 hours ago
    There is an internal sp_MSforeachdb, and maybe you can use it:
    https://learn.microsoft.com/en-us/answers/search.html?q=sp_MSforeachdb
    https://www.bing.com/search?q=sp_MSforeachdb

    Or use PowerShell with the module dbatools and execute it from outside SQL Server...

    Get-DbaDatabase -SqlInstance "server1" | Invoke-DbaQuery -File "C:\scripts\sql\rebuild.sql"  
    

    Invoke-DbaQuery

    3 people found this answer helpful.
    0 comments No comments

  2. Olaf Helper 47,436 Reputation points
    2022-10-27T07:14:07.237+00:00

    With one DELETE statement you can address one table, not more, so "one single query" is not possible.


  3. Jingyang Li 5,896 Reputation points Volunteer Moderator
    2022-10-27T13:54:52.813+00:00

    Can you try Truncate command for your query?

    use [A];

    Truncate table DeviceCommands;
    --or
    Truncate table [A].dbo.DeviceCommands;

    If it runs faster, you can apply to all databases with an option you choose from other replies.

    0 comments No comments

  4. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2022-10-27T21:17:16.813+00:00
       DECLARE @sql nvarchar(MAX)  
         
       SELECT @sql = string_agg('DELETE ' + quotename(DB) + '.dbo.DeviceCommands', char(13) + char(10))  
       FROM  (VALUES('Db1'),('Db2'),('Db3')) AS D(DB)  
         
       PRINT @sql  
         
       EXEC (@sql)  
    

    You could also put the DB names in a fixed table. If it's all tables on the server but the system databases, you could use sys.databses WHERE database_id > 5.

    0 comments No comments

  5. Seeya Xi-MSFT 16,586 Reputation points
    2022-10-28T02:57:31.98+00:00

    Hi @Devendra Kumar Sahu ,

    Agree with them. You can use sp_MSforeachdb.
    Please refer to this blog: Run same command on all SQL Server databases without cursors
    Hope this will give you some help.

    Best regards,
    Seeya


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.