Still use SQL Server Database Mirroring - List all DBs and status for alerting

List all Mirrored Databases and status on a SQL Server Instance:

To list all databases in a SQL Server instance with a little information or even a lot of information for all databases configured for mirroring use the following queries:
Reminder 
The mirroring_state numbers represent the following:
0 = Suspended
1 = Disconnected from the other partner
2 = Synchronizing 
3 = Pending Failover
4 = Synchronized
5 = The partners are not synchronized. Failover is not possible now

Below is a comparison and methodology for stepping through example until you refine what you want to see. 
Enjoy!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

USE master
GO

SELECT d.name AS Database_Name, CASE
                                     WHEN dm.mirroring_state is NULL THEN 'Not Mirrored'
                                     ELSE 'Mirrored' END AS Mirroring_Status
  FROM sys.databases d
  JOIN sys.database_mirroring dm ON d.database_id = dm.database_id
 ORDER BY d.name;
GO

--Query for Database Name, Mirroring status and state for all databases on an instance:
--DISCONNECTED, SYNCHRONIZED, SYNCHRONIZING, PENDING_FAILOVER, SUSPENDED, UNSYNCHRONIZED.

SELECT d.name AS Database_Name, CASE
                                     WHEN dm.mirroring_state is NULL THEN 'Not Mirrored'
                                     ELSE 'Mirrored' END AS Mirroring_Status
      ,dm.mirroring_state_desc as Mirroring_State
  FROM sys.databases d
  JOIN sys.database_mirroring dm ON d.database_id = dm.database_id
 ORDER BY d.name;
GO

-- Get more refinded to remove databases not participating in mirroring
SELECT d.name AS Database_Name, dm.mirroring_state AS Mirroring_Status
      ,dm.mirroring_state_desc as Mirroring_State
  FROM sys.databases d
  JOIN sys.database_mirroring dm ON d.database_id = dm.database_id
  WHERE dm.mirroring_state_desc IS NOT null
GO

-- Returns the same thing as the temp table and cursor below…
SELECT TOP 1 dm.mirroring_state AS Mirroring_Status
  FROM sys.databases d
  JOIN sys.database_mirroring dm ON d.database_id = dm.database_id
  WHERE dm.mirroring_state_desc IS NOT NULL
  ORDER BY dm.mirroring_state asc
GO
-- Pick which is easier and better for your use case
-- This was created for use with RedGate SQL monitor database mirroring alert 
USE [msdb];
GO
-- Drop #Tmp_tblDatabaseInfo table if it exists
IF OBJECT_ID( 'tempdb..#Tmp_tblDatabaseName' ) IS NOT NULL
    DROP TABLE #Tmp_tblDatabaseName;

-- Create #Tmp_tblDatabaseInfo table for population
CREATE TABLE #Tmp_tblDatabaseName ([DBName] [nvarchar](254) NULL) ON [PRIMARY];

-- Populate data info from sys.master_files and sys.databases
INSERT INTO #Tmp_tblDatabaseName (DBName)
SELECT d.name AS Database_Name
FROM sys.databases d JOIN sys.database_mirroring dm
ON d.database_id=dm.database_id
WHERE dm.mirroring_state is NOT NULL
ORDER BY d.name
--SELECT * FROM #Tmp_tblDatabaseName

-- Declare variables
declare @databaseName nvarchar(254);
-- local means the cursor name is private to this code
-- fast_forward enables some speed optimizations
declare MirroringDBs cursor local fast_forward for
    select DBName FROM #Tmp_tblDatabaseName
         order BY DBName asc
        
DECLARE       @MonitorResults AS TABLE (database_name VARCHAR(255),role INT,mirror_state TINYINT,witness_status TINYINT,log_generat_rate INT,unsent_log INT,sent_rate INT,
unrestored_log INT,recovery_rate INT,transaction_delay INT,transaction_per_sec INT,average_delay INT,time_recorded DATETIME,time_behind DATETIME,local_time DATETIME);

open MirroringDBs;

while 1 = 1
    BEGIN
        fetch next from MirroringDBs
         into @databaseName;

        if @@fetch_status <> 0 begin
break;
end;
              INSERT INTO @MonitorResults EXEC sp_dbmmonitorresults @database_name = @databaseName, @mode = 0, @update_table = 0;
    END;
---- Return one result if other than 4 then this will mean Redgate should alert otherwise a 4 is all good
SELECT TOP 1 mirror_state FROM @MonitorResults ORDER BY mirror_state asc;
---- Uncomment the below line if you want to see more/all data points
--SELECT * FROM @MonitorResults ORDER BY mirror_state asc;
close MirroringDBs;
deallocate MirroringDBs;
GO

Comments

Popular posts from this blog

How to mass update all or some sql server agent job step commands

SQL Monitoring for Blocking and Locking Capture and Log to table with sp_WhoIsActive or sp_BlitzWho

SQL Server General Index Naming Convention