How to test linkedserver's connectivity in TSQL

后端 未结 2 1884
情歌与酒
情歌与酒 2021-02-10 14:42

I need to write a procedure to collect data from several remote servers,

I use linkedservers and OPENQUERY to gather data from servers, but sometimes I lose connection

相关标签:
2条回答
  • 2021-02-10 14:56

    You can use below script

    https://web.archive.org/web/20190201090243/https://blogs.msdn.microsoft.com/sqltips/2005/06/07/test-linked-server-connection-settings/

    declare @srvr nvarchar(128), @retval int;
    set @srvr = 'my_linked_srvr';
    begin try
        exec @retval = sys.sp_testlinkedserver @srvr;
    end try
    begin catch
        set @retval = sign(@@error);
    end catch;
    if @retval <> 0
      raiserror('Unable to connect to server. This operation will be tried later!', 16, 2 );
    
    0 讨论(0)
  • 2021-02-10 15:09
    USE master;  
    GO  
    sp_testlinkedserver [LINKED_SERVER_NAME];  
    GO 
    

    or

    SELECT * FROM OPENQUERY(LINKED_SERVER_NAME , 'select name DATABASE_NAME ,SYS_CONTEXT (''USERENV'', ''SESSION_USER'') USERNAME, host_name from v$database, dual, v$instance') ;
    
    0 讨论(0)
提交回复
热议问题