How to step through linked servers after failing to connect to one I would like to change my stored procedure so it continues executing after failing to connect to a linked server. How could I write this stored procedure better?

I am using TRY...CATCH to query servers that I am creating from a list to track transaction log backup history.

ALTER PROC [dbo].[spLogBackup]

AS

TRUNCATE TABLE dbo.tLogBackup

DECLARE servers_cursor CURSOR
FOR
select  distinct LinkedserverName
from dbo.Environment
join master..sysservers on srvname COLLATE DATABASE_DEFAULT  = 
LinkedserverName COLLATE DATABASE_DEFAULT
where LinkedServerName not in ('TestDB')

and ServerUse in ('PROD', 'DR')
and IncludeInstats = 1
order by LinkedServerName

OPEN servers_cursor
DECLARE @Servername varchar(250)

DECLARE @sql varchar(8000)

FETCH NEXT FROM servers_cursor INTO @ServerName
WHILE (@@FETCH_STATUS = 0)
BEGIN TRY
--Looping throught the servers.
set @sql = 'insert into dbo.tLogBackup (servername, rundate, runtime, 
jobname, Status, CurrentStatus, enabled) select ''' + @ServerName + 
''',last_run_date, last_run_time, name, last_run_outcome, 
 current_execution_status, enabled
 from openquery([' + @ServerName + '],''exec 
 msdb.dbo.sp_get_composite_job_info'' )'
 print @ServerName
exec (@sql)
--SELECT (@sql)
FETCH NEXT FROM servers_cursor INTO @ServerName
END TRY

BEGIN CATCH  
SELECT  
    ERROR_NUMBER() AS ErrorNumber  
    ,ERROR_SEVERITY() AS ErrorSeverity  
    ,ERROR_STATE() AS ErrorState  
    ,ERROR_PROCEDURE() AS ErrorProcedure  
    ,ERROR_MESSAGE() AS ErrorMessage;  
END CATCH; 

CLOSE servers_cursor
DEALLOCATE servers_cursor
You just need to add the BEGIN/END for your WHILE LOOP and move the FETCH NEXT outside of the TRY/CATCH

ALTER PROC [dbo].[spLogBackup]

AS

TRUNCATE TABLE dbo.tLogBackup

DECLARE servers_cursor CURSOR
FOR
select  distinct LinkedserverName
from dbo.Environment
join master..sysservers on srvname COLLATE DATABASE_DEFAULT  = 
LinkedserverName COLLATE DATABASE_DEFAULT
where LinkedServerName not in ('TestDB')

and ServerUse in ('PROD', 'DR')
and IncludeInstats = 1
order by LinkedServerName

OPEN servers_cursor
DECLARE @Servername varchar(250)

DECLARE @sql varchar(8000)

FETCH NEXT FROM servers_cursor INTO @ServerName
WHILE (@@FETCH_STATUS = 0)
BEGIN

BEGIN TRY
--Looping throught the servers.
set @sql = 'insert into dbo.tLogBackup (servername, rundate, runtime, 
jobname, Status, CurrentStatus, enabled) select ''' + @ServerName + 
''',last_run_date, last_run_time, name, last_run_outcome, 
 current_execution_status, enabled
 from openquery([' + @ServerName + '],''exec 
 msdb.dbo.sp_get_composite_job_info'' )'
 print @ServerName
exec (@sql)
--SELECT (@sql)

END TRY

BEGIN CATCH  
SELECT  
    ERROR_NUMBER() AS ErrorNumber  
    ,ERROR_SEVERITY() AS ErrorSeverity  
    ,ERROR_STATE() AS ErrorState  
    ,ERROR_PROCEDURE() AS ErrorProcedure  
    ,ERROR_MESSAGE() AS ErrorMessage;  
END CATCH; 

FETCH NEXT FROM servers_cursor INTO @ServerName

END

CLOSE servers_cursor
DEALLOCATE servers_cursor