【发布时间】:2015-09-29 00:13:37
【问题描述】:
我有一个 C# 方法来执行 SQL 作业。它成功地执行了 SQL 作业。 并且代码运行良好。
我为此使用标准 SQL 存储过程 msdb.dbo.sp_start_job。
这是我的代码..
public int ExcecuteNonquery()
{
var result = 0;
using (var execJob =new SqlCommand())
{
execJob.CommandType = CommandType.StoredProcedure;
execJob.CommandText = "msdb.dbo.sp_start_job";
execJob.Parameters.AddWithValue("@job_name", "myjobname");
using (_sqlConnection)
{
if (_sqlConnection.State == ConnectionState.Closed)
_sqlConnection.Open();
sqlCommand.Connection = _sqlConnection;
result = sqlCommand.ExecuteNonQuery();
if (_sqlConnection.State == ConnectionState.Open)
_sqlConnection.Close();
}
}
return result;
}
这是在作业中执行的 sp
ALTER PROCEDURE [Area1].[Transformation]
AS
BEGIN
SET NOCOUNT ON;
SELECT NEXT VALUE FOR SQ_COMMON
-- Transform Master Data
exec [dbo].[sp_Transform_Address];
exec [dbo].[sp_Transform_Location];
exec [dbo].[sp_Transform_Product];
exec [dbo].[sp_Transform_Supplier];
exec [dbo].[sp_Transform_SupplierLocation];
-- Generate Hierarchies and Product References
exec [dbo].[sp_Generate_HierarchyObject] 'Area1',FGDemand,1;
exec [dbo].[sp_Generate_HierarchyObject] 'Area1',RMDemand,2;
exec [dbo].[sp_Generate_Hierarchy] 'Area1',FGDemand,1;
exec [dbo].[sp_Generate_Hierarchy] 'Area1',RMDemand,2;
exec [dbo].[sp_Generate_ProductReference] 'Area1',FGDemand,1;
exec [dbo].[sp_Generate_ProductReference] 'Area1',RMDemand,2;
-- Transform Demand Allocation BOM
exec [Area1].[sp_Transform_FGDemand];
exec [Area1].[sp_Transform_FGAllocation];
exec [Area1].[sp_Transform_RMDemand];
exec [Area1].[sp_Transform_RMAllocation];
exec [Area1].[sp_Transform_BOM];
exec [Area1].[sp_Transform_RMDemand_FK];
-- Transform Purchasing Document Data
exec [dbo].[sp_Transform_PurchasingDoc];
exec [dbo].[sp_Transform_PurchasingItem];
exec [dbo].[sp_Transform_ScheduleLine];
exec [dbo].[sp_CalculateRequirement] 'Area1'
exec [dbo].[sp_Create_TransformationSummary] 'Area1'
-- Trauncate Integration Tables
exec [dbo].[sp_TruncateIntegrationTables] 'Area1'
END
问题是,即使作业执行成功与否,它总是返回-1。如何确定作业是否成功执行。
【问题讨论】:
-
你能告诉我们你的StoredProcedure吗?
-
这是一个标准的sql
-
如果你有 SET NOCOUNT ON;一开始,您的返回结果将始终为 -1,因为您在过程中关闭了有关受影响行的信息,所以这很重要。
-
要监控作业状态,您可以使用存储过程
MSDB.dbo.sp_help_job。参见例如How to Execute and Monitor an Agent Job Using T-SQL in SQL Server 2005/2008。只需将其包装在 .NET 代码中即可。 -
我同意 MicSim,因为这些作业是异步的,SQL 在某处注册这些作业,我认为它在 sysjobhistory 中?因此,当 SQL 将作业状态更新为成功或失败时,您必须等待。
标签: c# sql stored-procedures ado.net