Executing SQL query on multiple databases

前端 未结 7 1571
无人共我
无人共我 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 20:05

    You can write script like this

    DECLARE CURSOR_ALLDB_NAMES CURSOR FOR
    SELECT name 
    FROM Sys.Databases
    WHERE name NOT IN('master', 'tempdb') 
    
    OPEN CURSOR_ALLDB_NAMES
    
    FETCH CURSOR_ALLDB_NAMES INTO @DB_NAME
    
    WHILE @@Fetch_Status = 0
    BEGIN
      EXEC('UPDATE '+ @DB_NAME + '..SameTableNameAllDb SET Status=1')
      FETCH CURSOR_ALLDB_NAMESINTO INTO @DB_NAME
    END
    
    CLOSE CURSOR_ALLDB_NAMES
    
    0 讨论(0)
提交回复
热议问题