¿TRY / CATCH no funciona en el error del Agente SQL Server?
yo suelosp_start_job
para comenzar un trabajo
El trabajo (test2
) tiene un solo paso:
select getdate()
waitfor delay '00:00:10'
losTRY/CATCH
código:
begin try
EXEC msdb.dbo.sp_start_job @job_name = 'test2'
end try
begin catch
print 'error'
end catch
Primera ejecución del código:
El trabajo 'test2' comenzó con éxito.
Segunda ejecución del código (dentro de 10 segundos):
Mensaje 22022, Nivel 16, Estado 1, Línea 0
Error SQLServerAgent: Solicitud para ejecutar la prueba de trabajo2 (del Usuario sa) rechazada porque el trabajo ya se está ejecutando desde una solicitud del Usuario sa.
Por queTRY/CATCH
no funciona en este escenario?
ACTUALIZACIÓN: Debería agregar al principio que estoy trabajando en un servidor SQL 2005 que tiene servidores vinculados (servidor SQL 2000). Estaba intentando escribir un proc en el servidor SQL Server 2005 para ver un trabajo en todos los servidores vinculados. Si el trabajo no se está ejecutando, ejecútelo. Inicialmente, utilicé try-catch y esperé detectar cualquier error cuando ejecuté el trabajo que ya estaba en ejecución pero falló (este hilo).
Finalmente utilicé el siguiente código: (no compilará, necesitas sustituir algunas variables, solo da una idea)
CREATE TABLE [dbo].[#jobInfo](
[job_id] [uniqueidentifier] NULL,
[originating_server] [nvarchar](30) ,
[name] [nvarchar](128) ,
[enabled] [tinyint] NULL,
[description] [nvarchar](512) ,
[start_step_id] [int] NULL,
[category] [nvarchar](128) ,
[owner] [nvarchar](128) ,
[notify_level_eventlog] [int] NULL,
[notify_level_email] [int] NULL,
[notify_level_netsend] [int] NULL,
[notify_level_page] [int] NULL,
[notify_email_operator] [nvarchar](128) ,
[notify_netsend_operator] [nvarchar](128) ,
[notify_page_operator] [nvarchar](128) ,
[delete_level] [int] NULL,
[date_created] [datetime] NULL,
[date_modified] [datetime] NULL,
[version_number] [int] NULL,
[last_run_date] [int] NOT NULL,
[last_run_time] [int] NOT NULL,
[last_run_outcome] [int] NOT NULL,
[next_run_date] [int] NOT NULL,
[next_run_time] [int] NOT NULL,
[next_run_schedule_id] [int] NOT NULL,
[current_execution_status] [int] NOT NULL,
[current_execution_step] [nvarchar](128) ,
[current_retry_attempt] [int] NOT NULL,
[has_step] [int] NULL,
[has_schedule] [int] NULL,
[has_target] [int] NULL,
[type] [int] NOT NULL
)
SET @sql =
'INSERT INTO #jobInfo
SELECT * FROM OPENQUERY( [' + @srvName + '],''set fmtonly off exec msdb.dbo.sp_help_job'')'
EXEC(@sql)
IF EXISTS (select * from #jobInfo WHERE [name] = @jobName AND current_execution_status IN (4,5)) -- 4: idle, 5: suspended
BEGIN
SET @sql = 'EXEC [' + @srvName + '].msdb.dbo.sp_start_job @job_name = ''' + @jobName + ''''
--print @sql
EXEC (@sql)
INSERT INTO #result (srvName ,status ) VALUES (@srvName, 'Job started.')
END ELSE BEGIN
INSERT INTO #result (srvName ,status ) VALUES (@srvName, 'Job is running already. No action taken.')
END