2014-09-24 49 views
0

这里是我的代码:Try-Catch:我如何捕获一个值来记录它?

BEGIN TRY 
update [MicrosoftDynamicsAX].[dbo].[SALESLINE] 
     set recid = i.recid 
    FROM [Integration].[dbo].[CrmOrderDetails] c WITH(NOLOCK) 
    left outer join [MicrosoftDynamicsAX].[dbo].[SALESTABLE] s WITH(NOLOCK) 
     on cast(c.salesorderid as varchar(50)) = s.CRMGUID 
    left outer join [MicrosoftDynamicsAX].[dbo].[INVENTTABLE] it WITH(NOLOCK) 
     on it.NAMEALIAS = c.productidname  
    left outer join [MicrosoftDynamicsAX].[dbo].[INVENTTABLEMODULE] i WITH(NOLOCK) 
     on it.ITEMID = i.ITEMID 
     and i.MODULETYPE = 2 -- sales order 
    left outer join [MicrosoftDynamicsAX].[dbo].[SALESLINE] sl WITH(NOLOCK) 
     on sl.SALESID = s.SALESID 
     and sl.ITEMID = it.itemid 
END TRY 
BEGIN CATCH 
INSERT INTO [Integration].[dbo].[PackageError] 
      ([ID] 
      ,[PackageName] 
      ,[PackageStep] 
      ,[ErrorDescription]) 
    SELECT 'RECID VALUE HERE', 'CrmToAxOrders', 'UpdateSalesLineSP', ERROR_MESSAGE() ; 

END CATCH 

如何获得i.recid值错误,所以我可以登录到PackageError表?我没有看到确定哪个recid导致错误的方法。

谢谢

回答

0

你知道你会/接受哪些错误吗?如果您知道错误,您甚至可以在try/catch之前搜索损坏的记录。

请注意,您的查询使用左连接,并且不会将更新表链接到记录。 是否可以重新设计,以便它只更新找到的记录。

update s1 
    set recid = i.recid 
FROM [Integration].[dbo].[CrmOrderDetails] c 
inner join [MicrosoftDynamicsAX].[dbo].[SALESTABLE] s 
    on cast(c.salesorderid as varchar(50)) = s.CRMGUID 
inner join [MicrosoftDynamicsAX].[dbo].[INVENTTABLE] it 
    on it.NAMEALIAS = c.productidname  
inner join [MicrosoftDynamicsAX].[dbo].[INVENTTABLEMODULE] i 
    on it.ITEMID = i.ITEMID 
    and i.MODULETYPE = 2 -- sales order 
inner join [MicrosoftDynamicsAX].[dbo].[SALESLINE] sl 
    on sl.SALESID = s.SALESID 
    and sl.ITEMID = it.itemid 

这样它更新链接S1的记录,且仅当它们在其他表中发现。 此外,使用WITH(NOLOCK)将检索尚未提交到数据库的记录,只调整已提交的值更安全。

相关问题