T-SQL查找是否在事务复制中的订阅服务器上订阅了数据库

时间:2013-06-05 17:12:35

标签: sql-server sql-server-2008 transactional-replication

T-SQL查找是否在事务复制中的订阅服务器上订阅了数据库。我不想查询分发的详细信息。

以下不起作用。

SELECT is_subscribed FROM sys.databases

SELECT DATABASEPROPERTYEX('database', 'IsSubscribed')

3 个答案:

答案 0 :(得分:8)

到目前为止,这是我发现的最佳方式,但我不确定这是否适用于非事务复制。

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

答案 1 :(得分:0)

上面的查询将起作用,除非有一些非在线数据库,例如RESTORING,则查询将失败。最好将可用数据库列表复制到temp表中,然后对它运行逻辑:

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

答案 2 :(得分:0)

首先您得到Aaron Bertrand's sp_foreachdb replacement for sp_MSforeachdb

然后在以下脚本中使用它:

---------------------------------------------------------
-- 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

这将为您提供以下信息: enter image description here