这是我的代码:
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 :(得分: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)将检索尚未提交到数据库的记录,仅调整已提交的值更安全。