if exists ( select * from sys.procedures p where p.name = 'sp_SetLoadErrors' ) DROP PROCEDURE [dbo].[sp_SetLoadErrors]
GO

CREATE PROCEDURE [dbo].[sp_SetLoadErrors]
 @SLHID uniqueidentifier
AS 
BEGIN 
INSERT INTO [IES].T_SLH_ERROR
(SlhErrorID, [Count], [ExpertiseError], SchetLoadHistory)
select NEWID(), count(*), aie.ExpertiseError, @SLHID
from [IES].T_SCHET_LOAD_HISTORY slh with(nolock) 
join [IES].T_SCHET s with(nolock)  on (slh.Schet = s.SchetID)
join [IES].T_SCHET_ZAP sz with(nolock)  on (sz.Schet = s.SchetID)
join [IES].T_SCHET_SLUCH ss with(nolock)  on (sz.SchetZapID = ss.SchetZap)
join [IES].T_SCHET_ACT_ITEM sai with(nolock)  on (ss.SchetSluchID = sai.SchetSluch)
join [IES].T_ACT_ITEM_ERROR aie with(nolock)  on (sai.SchetActItemID = aie.SchetActItem)
where slh.LoadHistoryID = @SLHID
GROUP BY aie.ExpertiseError

END