检查复制是否已启用

时间:2011-11-08 01:59:57

标签: sql-server tsql replication

检查是否以SQL 2000,2005和2008兼容格式启用了复制的最简单方法是什么?不需要任何状态信息...只是为了知道它是否已启用数据库。我需要这个,因为如果在尝试执行架构更新之前启用了复制,我希望我的Db Migration代码抛出异常。

感谢。

4 个答案:

答案 0 :(得分:6)

我发现了这个查询,但这只适用于SQL 2005及更高版本(不适用于SQL 2000版本)。

T-SQL查询

  select name, is_published, is_subscribed, is_merge_published, is_distributor
  from sys.databases
  where is_published = 1 or is_subscribed = 1 or
  is_merge_published = 1 or is_distributor = 1

答案 1 :(得分:0)

在查看本文时,MSDN说“当用户数据库配置为发布服务器或订阅服务器时,复制会将系统表添加到数据库。从复制中删除用户数据库时,将删除这些表拓扑“

您可以查找文章中列出的其中一个表作为复制证明,并相应地继续查询。

答案 2 :(得分:0)

来自:www.mssqlinsider.com/2013/09/check-databases-part-replication

SELECT 
    name as [Database name],
    CASE is_published 
        WHEN 0 THEN 'No' 
        ELSE 'Yes' 
        END AS [Is Published],
    CASE is_merge_published 
        WHEN 0 THEN 'No' 
        ELSE 'Yes' 
        END AS [Is Merge Published],
    CASE is_distributor 
        WHEN 0 THEN 'No' 
        ELSE 'Yes' 
        END AS [Is Distributor],
    CASE is_subscribed 
        WHEN 0 THEN 'No' 
        ELSE 'Yes' 
        END AS [Is Subscribed]
FROM sys.databases
WHERE database_id > 4

答案 3 :(得分:0)

要直接回答您的问题,请使用:

SELECT COUNT(*) AS IsPublished
FROM  sys.databases
WHERE (is_published | is_subscribed | is_merge_published) AND (name = @dbname)

这将为您提供已发布数据库的列表。您可以通过添加is_distributor

来选择包括分发数据库。
SELECT name
FROM  sys.databases
WHERE (is_published | is_subscribed | is_merge_published | is_distributor = 1)

对我而言,所有值得做的事情都值得再次做,因此,我创建了一个用户定义的函数(您可以轻松地使用存储过程):

-- ================================================
-- Author:      J. Duke Rogers
--              Communicore Technologies &
--              Triangle Forensics
-- Create date: 07.25.20
-- Description: If @dbname is null or '' it uses 
--              the database in which it is defined
-- WARNING:     May cause black hole(s)
-- ================================================
CREATE FUNCTION fn_IsReplicationEnabled 
(
    @dbname sysname =NULL
)
RETURNS bit
AS
BEGIN
    DECLARE @Result int

    IF (@dbname is null or @dbname='')
        set @dbname=DB_NAME()

    SELECT @Result=COUNT(*)
        FROM  
            sys.databases
        WHERE 
            is_published | is_subscribed | is_merge_published=1 AND [name] = @dbname
    
    IF @Result is null or @Result <0 or @Result>1
        return CAST('Something impossible has occurred, singularity and/or black hole
                     imminent, don helmet!' as bit)
    
    RETURN IIF(@Result=1,1,0)

END
GO

恕我直言