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

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[sp_R010ISKEXLoad]
 @Xml xml
 ,@FileName varchar(50)
AS 
BEGIN

	select 
		 T.c.value('./Kod[1]', 'int') AS Kod
		,T.c.value('./Opis[1]', 'varchar(255)') AS Opis
		,case when T.c.value('./DATEBEG[1]', 'varchar(20)')='' then null else CONVERT(datetime,T.c.value('./DATEBEG[1]', 'varchar(20)'),104) end AS DATEBEG
		,case when T.c.value('./DATEEND[1]', 'varchar(20)')='' then null else CONVERT(datetime,T.c.value('./DATEEND[1]', 'varchar(20)'),104) end AS DATEEND
	into #R010
	from @xml.nodes('/packet/zap') T(c)
	
	update t2 
	set  t2.Opis=t1.Opis
	from #R010 t1
	join [IES].T_R010_ISKEX t2 on (t1.Kod = t2.Kod)

	insert into [IES].T_R010_ISKEX(Kod, Opis, DATEBEG, DATEEND)
	select t1.Kod, t1.Opis, t1.DATEBEG, t1.DATEEND
	from #R010 t1
	left join [IES].T_R010_ISKEX t2 on (t1.Kod = t2.Kod)
	where t2.Kod is null

	drop table #R010

END

