检查链接服务器的DBStatus

时间:2019-11-11 15:36:49

标签: sql-server linked-server

我想确定在LSNR_ADMINDB的数据库DBConfig中注册的所有SQL数据库在相应的链接服务器上是否都可用,并且在那里联机还是脱机。

我有以下代码,但是当我运行它时,它返回NULL值。我究竟做错了什么。谁能帮忙

DECLARE @STR_DBServer NVARCHAR(128)
DECLARE @STR_DBName NVARCHAR(128)
DECLARE @state_desc NVARCHAR(10)
DECLARE @table table (Server nvarchar(128), Datenbank nvarchar(128), Status nvarchar(10))  

DECLARE CUR1 CURSOR FOR
        SELECT 
        STR_DBServer , STR_DBName  
        FROM   DBConfig.dbo.T_Datenbanken d 
        where int_systemID = 1 
        order by    STR_DBServer 

OPEN CUR1
FETCH NEXT FROM CUR1 INTO @STR_DBServer, @STR_DBName

WHILE @@FETCH_STATUS = 0
BEGIN
    begin try

        exec sys.sp_testlinkedserver @STR_DBServer  
        If Exists ( select s.name, 
        m.state_desc from master.sys.databases m, master.sys.servers s
        where is_linked= 1) 
        insert into @table (Status)
        values
        (@state_desc) 

    end try

    begin catch

          print 'Keine verbindung möglich mit '  + '' + @STR_DBName + ' auf ' + @STR_DBServer 

    end catch 

FETCH NEXT FROM CUR1 INTO @STR_DBServer, @STR_DBName
END
CLOSE CUR1
DEALLOCATE CUR1
select * from @table

0 个答案:

没有答案