执行时存储过程 运行s 但在代理作业中不 运行 即使它成功
Stored procedure runs when exec but doesn't run in agent job even though it's successful
我读过很多其他人也有类似的问题,但到目前为止我发现没有任何效果。
这是一个 SQL Server 2008 R2,我们有一个代理作业,每天 运行 一次,用数据填充 table。当 运行 一个新查询和 exec sp 它工作正常但是当我们 运行 它作为代理作业时它成功完成但在按日期查看最新数据时实际上并没有填充任何数据。
帐户 运行ning sql 代理是系统管理员,我还尝试使用 运行 以用户身份执行的 SQL 帐户对其进行 运行ning(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;
有人有什么建议吗?
我添加了
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 的帮助
我读过很多其他人也有类似的问题,但到目前为止我发现没有任何效果。 这是一个 SQL Server 2008 R2,我们有一个代理作业,每天 运行 一次,用数据填充 table。当 运行 一个新查询和 exec sp 它工作正常但是当我们 运行 它作为代理作业时它成功完成但在按日期查看最新数据时实际上并没有填充任何数据。 帐户 运行ning sql 代理是系统管理员,我还尝试使用 运行 以用户身份执行的 SQL 帐户对其进行 运行ning(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;
有人有什么建议吗?
我添加了
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 的帮助