-
Notifications
You must be signed in to change notification settings - Fork 10
/
prc_Repl_ReplicationSubscribers.sql
119 lines (103 loc) · 3.53 KB
/
prc_Repl_ReplicationSubscribers.sql
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
IF NOT EXISTS(SELECT 1 FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_NAME = 'prc_Repl_ReplicationSubscribers' And ROUTINE_SCHEMA = 'dbo')
BEGIN
EXEC('CREATE Procedure dbo.prc_Repl_ReplicationSubscribers as raiserror(''Empty Stored Procedure!!'', 10, 1) with seterror')
IF (@@error = 0)
PRINT 'Successfully created empty stored procedure dbo.prc_Repl_ReplicationSubscribers.'
ELSE
BEGIN
PRINT 'FAILED to create stored procedure dbo.prc_Repl_ReplicationSubscribers.'
END
END
GO
ALTER PROCEDURE [dbo].[prc_Repl_ReplicationSubscribers]
AS
BEGIN
SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
BEGIN TRY
-- SUBSCRIPTIONS
CREATE TABLE #tmp_subscriptions (
publisher sysname not null,
publisher_db sysname not null,
publication sysname null,
replication_type int not NULL,
subscription_type int not NULL,
last_updated datetime null,
subscriber_db sysname not null,
update_mode smallint null,
last_sync_status int null,
last_sync_summary sysname null,
last_sync_time datetime null
)
DECLARE @DBID int
,@DBName sysname
,@DBStatus int
,@Incr smallint
,@MaxIncr smallint
,@DBMode varchar(60)
,@StatusMsg varchar(250)
,@script nvarchar(MAX)
DECLARE @DBList Table (ID smallint Identity(1,1), DBName sysname, [DBID] int, DBStatus int)
Insert into @DBList (DBName, [DBID], DBStatus)
SELECT name, dbid, status
FROM master.dbo.sysdatabases
WHERE [name] NOT IN ('tempdb', 'model')
Select @MaxIncr = @@RowCount, @Incr = 1
--Select * from @DBList
While @Incr <= @MaxIncr
BEGIN
--Set variables
SELECT @DBName = DBName, @DBID = [DBID], @DBStatus = DBStatus
From @DBList
Where ID = @Incr
--Check Database Accessibility
SELECT @DBMode = 'OK'
IF DATABASEPROPERTY(@DBName, 'IsDetached') > 0
SELECT @DBMode = 'Detached'
ELSE IF DATABASEPROPERTY(@DBName, 'IsInLoad') > 0
SELECT @DBMode = 'Loading'
ELSE IF DATABASEPROPERTY(@DBName, 'IsNotRecovered') > 0
SELECT @DBMode = 'Not Recovered'
ELSE IF DATABASEPROPERTY(@DBName, 'IsInRecovery') > 0
SELECT @DBMode = 'Recovering'
ELSE IF DATABASEPROPERTY(@DBName, 'IsSuspect') > 0
SELECT @DBMode = 'Suspect'
ELSE IF DATABASEPROPERTY(@DBName, 'IsOffline') > 0
SELECT @DBMode = 'Offline'
ELSE IF DATABASEPROPERTY(@DBName, 'IsEmergencyMode') > 0
SELECT @DBMode = 'Emergency Mode'
ELSE IF DATABASEPROPERTY(@DBName, 'IsShutDown') > 0
SELECT @DBMode = 'Shut Down (problems during startup)'
IF @DBMode <> 'OK'
BEGIN
Set @StatusMsg = 'Skipping database ' + @DBName + ' - Database is in ' + @DBMode + ' state.'
PRINT @StatusMsg
Goto NextDB
END
--Put Code here for executing on each of the databases.
SET @script = 'EXEC [' + @DBName + '].sys.sp_MSenumsubscriptions @subscription_type = ''both'', @reserved = 1'
EXEC (@script)
NextDB:
Set @Incr = @Incr + 1
END
SELECT publisher, publisher_db, publication
, CONVERT(varchar(20),CASE replication_type
when 0 then 'Transactional'
when 1 then 'Snapshot'
when 2 then 'Merge'
END) replication_type
, CONVERT(varchar(20),CASE subscription_type
when 0 then 'Push'
when 1 then 'Pull'
when 2 then 'Anonymous'
END) subscription_type
, last_updated, subscriber_db
, update_mode
, last_sync_status, last_sync_summary, last_sync_time
FROM #tmp_subscriptions
DROP TABLE #tmp_subscriptions
END TRY
BEGIN CATCH
PRINT 'Error catched. Please run the script step by step or contact a DBA.'
END CATCH
END