存储过程在exec时运行,但即使成功也不会在代理作业中运行

时间:2019-11-20 08:27:07

标签: sql-server sql-server-2008 stored-procedures sql-agent-job

我已经读过很多其他有类似问题的人,但是到目前为止我还没有找到任何有效的方法。 这是SQL Server 2008 R2,我们有一个代理作业,该作业每天运行一次,用数据填充表。当运行新查询和exec sp时,它运行良好,但是当我们将其作为代理作业运行时,它会成功完成,但按日期查看最新数据时实际上并不会填充任何数据。 运行sql代理的帐户是sysadmin,我也尝试过使用以用户身份执行的SQL帐户(SQL帐户是数据库上的db_owner)运行它。我尝试将TEXTSIZE 200000添加到该步骤。 我也进行了跟踪,从外观上看,存储过程实际上按预期运行

这是SP:

    /****** Object:  StoredProcedure [dbo].[CalculateKPIs_2]    Script Date: 2019-11-20 09:06:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[CalculateKPIs_2]
AS

          -- SET NOCOUNT ON added to prevent extra result sets from
          -- interfering with SELECT statements.
          SET NOCOUNT ON;

    -- Insert statements for procedure here
          -- Calculate daily totals per api resource
  INSERT INTO API_TRAFFIC_SUMMARY_STAT 
  SELECT CONVERT(VARCHAR(10), request_datetime, 20),
       api,
             api_version,
             method, 
       resource_path,
             SUM(request_count),
             SUM(fault_count),
             MIN(min_response_time),
       SUM(request_count * avg_response_time) / SUM(request_count),
       MAX(max_response_time),
       MIN(min_service_time),
       SUM(request_count * avg_service_time) / SUM(request_count),
       MAX(max_service_time)
  FROM api_traffic_summary
  WHERE CONVERT(VARCHAR(10), request_datetime, 20) = convert(VARCHAR(10), DATEADD(DAY, -1, GETDATE()), 20)
  GROUP BY CONVERT(VARCHAR(10), request_datetime, 20), api, api_version, method, resource_path;

  -- Calculate daily totals per api resource for each application
INSERT INTO API_TRAFFIC_SUMMARY_STAT_APPLICATION 
  SELECT CONVERT(VARCHAR(10), request_datetime, 20),
                   user_id,
                   application_name,
                   api,
                   api_version,
                   method, 
                   resource_path,
                   SUM(request_count),
                   SUM(fault_count),
                   MIN(min_response_time),
                   SUM(request_count * avg_response_time) / SUM(request_count),
                   MAX(max_response_time),
                   MIN(min_service_time),
                   SUM(request_count * avg_service_time) / SUM(request_count),
                   MAX(max_service_time)
  FROM api_traffic_summary
  WHERE CONVERT(VARCHAR(10), request_datetime, 20) = convert(VARCHAR(10), DATEADD(DAY, -1, GETDATE()), 20)
  GROUP BY CONVERT(VARCHAR(10), request_datetime, 20), user_id, application_name, api, api_version, method, resource_path;

-- Calculate daily fault totals per api resource for each application
INSERT INTO API_FAULT_SUMMARY_STAT_APPLICATION 
  SELECT CONVERT(VARCHAR(10), request_datetime, 20),
                   user_id,
                   application_name,
                   api,
                   api_version,
                   method, 
                   resource_path,
                   endpoint_address,
                   error_code,
                   SUM(fault_count)
  FROM api_fault_summary
  WHERE CONVERT(VARCHAR(10), request_datetime, 20) = convert(VARCHAR(10), DATEADD(DAY, -1, GETDATE()), 20) AND user_id IS NOT NULL AND application_name IS NOT NULL
  GROUP BY CONVERT(VARCHAR(10), request_datetime, 20), user_id, application_name, api, api_version, method, resource_path, endpoint_address, error_code;

有人有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我添加了

    IF @@ROWCOUNT = 0
    PRINT 'Something went wrong!'
ELSE PRINT 'Rows were updated...'

在每个Insert语句上,现在可以轻松地在代理作业历史记录中查看输出,如下所示:

    Rows were updated... 
[SQLSTATE 01000] (Message 0)  Rows were updated... 
[SQLSTATE 01000] (Message 0)  Rows were updated... 
[SQLSTATE 01000] (Message 0).  The step succeeded.

感谢@sepupic提供帮助