title: How to check if linked server is running within a function?
body: <p>I know that there is a way to check if the linked server is running.<br>
  I wanna know if there is a way to check it inside a function
  I wrote this code:</p>
  
  <pre><code>  create function dbo.Ck_DB_Stat
  (@servername nvarchar (20))
  returns bit
  AS
  BEGIN
  
      declare @srvr nvarchar(128), @retval int, @res nvarchar (10)
  
      declare @ck bit
      set @srvr = @servername;
      begin try
          exec @retval = sys.sp_testlinkedserver @srvr;
          set @ck = 1
      end try
  
      begin catch
          set @retval = sign(@@error);
      end catch;
  
      if @retval &lt;&gt; 0
          set @ck = 0
      return @ck
  END
  </code></pre>