Executing SQL query on multiple databases

前端 未结 7 1570
无人共我
无人共我 2021-01-02 19:25

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 o

相关标签:
7条回答
  • 2021-01-02 19:40

    this is the normal way of doing this :

    suppose you want to do a select on database DBOther than it would be :

    select * from DBOther..TableName
    

    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

    select * from DBOther.dbo.ViewName
    

    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:

    SELECT * FROM [AnotherServerName].[DB].[dbo].[Table]
    

    Here is another way that does not requires typing the database name :

    use DB1
    go
    
    select * from table1
    go
    
    use DB2
    go
    
    select * from table1
    go
    

    Note that this will only work if the tables and fields are exact the same on each database

    0 讨论(0)
  • 2021-01-02 19:48

    You can use the following script to run the same script on a set of databases. Just change the filter in the insert line.

    
    declare @dbs table (
      dbName varchar(100),
      done bit default 0
    )
    
    insert @dbs select [name], 0 FROM master.dbo.sysdatabases WHERE [Name] like 'targets_%'
    
    while (exists(select 1 from @dbs where done = 0))
    begin
      declare @db varchar(100);
      select top 1 @db = dbName from @dbs where done = 0;
      exec ('
        use [' + @db + '];
        update table1 set
          col1 = '''',
          col2 = 1
        where id = ''45b6facb-510d-422f-a48c-687449f08821''
      ');
      print @db + ' updated!';
      update @dbs set done = 1 where dbName = @db;
    end
    
    

    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.

    0 讨论(0)
  • 2021-01-02 19:52

    Depending on the requirement, you can do this:

    
    declare @dbName nvarchar(100)
    declare @script nvarchar(max)
    declare @dbIndex bigint = 0
    declare @dbCount bigint = (
        select count(*) from
            sys.databases
    
        )
    declare crs_databases cursor for
    (
        select
            [name]
        from
            sys.databases   
    )
    open crs_databases
    fetch next from crs_databases into @dbName
    while @@FETCH_STATUS = 0
    begin
        set @dbIndex = @dbIndex+1
        set @script = concat(@script,
            ' select Id from ['+@dbName+']..YourTableName ',        
            case 
                when @dbIndex = @dbCount then ''
                else 'union'
            end)
        fetch next from crs_databases into @dbName
    end
    
    select @script
    close crs_databases
    deallocate crs_databases
    
    

    Please note that the double dotted notation assumes that the schema is dbo. Otherwise, you need to explicitly write down the schema.

    select Id from ['+@dbName+'].schema.YourTableName
    

    When you need to execute stored procedures on each server, the @script variable will have another content.

    0 讨论(0)
  • 2021-01-02 19:53

    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.

    DECLARE @rn INT = 1, @dbname varchar(MAX) = '';
    WHILE @dbname IS NOT NULL 
    BEGIN
        SET @dbname = (SELECT name FROM (SELECT name, ROW_NUMBER() OVER (ORDER BY name) rn 
            FROM sys.databases WHERE name NOT IN('master','tempdb')) t WHERE rn = @rn);
    
        IF @dbname <> '' AND @dbname IS NOT NULL
            EXECUTE ('use ['+@dbname+'];
    
                /* Your script code here */
                UPDATE some_table SET ... ;
    
            ');
        SET @rn = @rn + 1;
    END;
    
    0 讨论(0)
  • 2021-01-02 20:01

    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:

    USE DB1;
    :r C:\SqlScript\YourLargeScript.sql
    GO
    USE DB2;
    :r C:\SqlScript\YourLargeScript.sql
    GO
    USE DB3;
    :r C:\SqlScript\YourLargeScript.sql
    GO
    
    0 讨论(0)
  • 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:

    0 讨论(0)
提交回复
热议问题