5

T-SQL to find if a Database is Subscribed on the Subscriber in Transactional Replication. I don't want to query Distribution for the details.

The following doesn't work.

SELECT is_subscribed FROM sys.databases

SELECT DATABASEPROPERTYEX('database', 'IsSubscribed')
5
  • "You can take a look at the MSsubscriptions table the distribution database, this might work for you...." I would like to query the Subscriber for this information not Distributor.
    – Afroz
    Jun 5, 2013 at 17:28
  • I'm not familiar with this function, might be usefull sp_MSenumsubscriptions
    – bummi
    Jun 5, 2013 at 17:39
  • According to the documentation, sys.databases.is_subscribed is "not used" so it's clear why it doesn't work. But databasepropertyex should work; does it always return zero?
    – Pondlife
    Jun 5, 2013 at 18:54
  • yes. DATABASEPROPERTYEX returns 0
    – Afroz
    Jun 5, 2013 at 21:04

3 Answers 3

9

So far this is the best way I found but I am not sure if this works in Non-Transactional Replication.

SELECT COALESCE(OBJECTPROPERTY(OBJECT_ID('dbo.MSreplication_objects'), 'IsMSShipped'),0) AS IsSubscribed

OR

SELECT name
FROM sys.databases
WHERE OBJECT_ID(name+'.dbo.MSreplication_objects') IS NOT NULL
0
0

The query above will work unless there are some databases that are not ONLINE, for example RESTORING then the query will fail. It's better to copy the list of available databases into temp table and then run the logic against it:

IF OBJECT_ID('tempdb..#db') IS NOT NULL DROP TABLE #db
SELECT name INTO #db FROM sys.databases WHERE state_desc = 'ONLINE' 
SELECT name FROM #db WHERE OBJECT_ID(name+'.dbo.MSreplication_objects') IS NOT NULL
0

first you get Aaron Bertrand's sp_foreachdb replacement for sp_MSforeachdb

then you use it in the script below:

---------------------------------------------------------
-- get all info about subscribers in a server
-- marcello miorelli
-- 23-dec-2017
---------------------------------------------------------
IF object_id('TEMPDB..#RADHE_SP2') IS NOT NULL
    DROP TABLE #RADHE_SP2

    create table #RADHE_SP2 (
        publisher sysname NOT NULL,  
        publisher_db sysname NOT NULL, 
        publication sysname NOT NULL, 
        replication_type int,
        subscription_type int,
        last_updated datetime,
        subscriberd_db sysname,
        update_mode int,
        last_sync_status int,
        last_sync_summary nvarchar(4000),
        last_sync_time datetime)


declare @db_list NVARCHAR(MAX)

SELECT @db_list  = STUFF((
                        SELECT ', ' + name
                         FROM sys.databases d
                        WHERE 1=1
                          and d.[state] = 0
                        FOR XML PATH(''), TYPE).value('.[1]', 'nvarchar(max)'), 1, 2, '')
--select @db_list

--exec sp_foreachdb @database_list = @db_list
--               ,@command='use ?; print db_name()'   


EXEC sp_foreachdb
@database_list = @db_list,
@command=
'use ?; 

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

insert into #RADHE_SP2
exec sp_MSenumsubscriptions

' 

SELECT * FROM #RADHE_SP2

that will give you something like this: enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.