我编写了一个 SQL 脚本来将名称映射到报告名称。我已经在我的本地 SQL Server 2012 机器上进行了验证。它运作良好。但是,当我在服务器中运行时,出现错误
消息 537,级别 16,状态 3,行 40
传递给 LEFT 或 SUBSTRING 函数的长度参数无效。
而且我还尝试直接在远程服务器中运行“选择”部分,结果也是正确的。我不明白为什么它是错误的。有什么想法吗?(注意:我在错误行中添加了“error here-->”)
DECLARE @jobTable TABLE
(
job_name nvarchar(max),
report_name nvarchar(max),
event_type nvarchar(max),
description nvarchar(max),
last_run_date int,
last_run_time int
)
declare
@job_name uniqueidentifier,
@report_name nvarchar(max),
@event_type nvarchar(max),
@description nvarchar(max),
@last_run_date int,
@last_run_time int,
@last_run_dur int
/************** alert ********/
declare getId_alertjob_cursor cursor for (
SELECT
[jobs].[name] job_name,
alertFeed.relativePath report_name,
CONVERT(nvarchar(max), SUBSTRING([steps].[command], CHARINDEX(' @EventType=''', [steps].[command], 0) + LEN(' @EventType='''), CHARINDEX('''', [steps].[command], CHARINDEX(' @EventType=''', [steps].[command]) + LEN(' @EventType=''')) - CHARINDEX(' @EventType=''', [steps].[command], 0) - LEN(' @EventType=''')), 0) event_type,
[steps].[last_run_date] last_date,
[steps].[last_run_time] last_time,
[steps].[last_run_duration] last_dur
FROM [msdb].[dbo].[sysjobs] [jobs] INNER JOIN [msdb].[dbo].[sysjobsteps] [steps] ON [jobs].[job_id] = [steps].[job_id], ReportingService_4c0ed75e1e8c4e50acbf853867f071f3_Alerting.dbo.AlertDefinition [alertDef], ReportingService_4c0ed75e1e8c4e50acbf853867f071f3_Alerting.dbo.Feed [alertFeed]
WITH (NOLOCK)
WHERE [jobs].[name] <> 'syspolicy_purge_history'
and alertDef.scheduleid = CONVERT(nvarchar(max), SUBSTRING([steps].[command], CHARINDEX(' @EventData=''', [steps].[command], 0) + LEN(' @EventData='''), CHARINDEX('''', [steps].[command], CHARINDEX(' @EventData=''', [steps].[command]) + LEN(' @EventData=''')) - CHARINDEX(' @EventData=''', [steps].[command], 0) - LEN(' @EventData=''')), 0)
and alertDef.FeedId = alertFeed.FeedId
);
open getId_alertjob_cursor
error here--> fetch next from getId_alertjob_cursor INTO @job_name, @report_name, @event_type, @last_run_date, @last_run_time, @last_run_dur
while @@FETCH_STATUS = 0
BEGIN
-- INSERT INTO @jobTable VALUES (@job_name, @report_name, @event_type,'Alert',0,0);
-- ,@last_run_date,@last_run_time);
fetch next from getId_alertjob_cursor INTO @job_name, @report_name, @event_type, @last_run_date, @last_run_time,@last_run_dur
END
close getId_alertjob_cursor
deallocate getId_alertjob_cursor