if exists ( select * from sys.procedures p where p.name = 'sp_F033SpmoLoad' ) DROP PROCEDURE [dbo].[sp_F033SpmoLoad]
GO
CREATE PROCEDURE [dbo].[sp_F033SpmoLoad]
 @Xml xml
 ,@FileName varchar(50)
AS 
BEGIN

  SELECT 
  T.c.value('./idspmo[1]', 'varchar(17)') AS IDSPMO
  ,T.c.value('./NAM_SPMO[1]', 'varchar(2000)') AS NAM_SPMO
  ,T.c.value('./NAM_SK_SPMO[1]', 'varchar(2000)') AS NAM_SK_SPMO
  ,T.c.value('./phone[1]', 'varchar(255)') AS PHONE
  ,T.c.value('./OSP[1]', 'varchar(1)') AS OSP
  ,T.c.value('./VID_SPMO[1]', 'varchar(2000)') AS VID_SPMO
  ,T.c.value('./OID_SPMO[1]', 'varchar(50)') AS OID_SPMO  
  ,case when T.c.value('./datebeg[1]', 'varchar(20)')='' then null else CONVERT(datetime,T.c.value('./datebeg[1]', 'varchar(20)'),103) end AS DATEBEG
  ,case when T.c.value('./dateend[1]', 'varchar(20)')='' then null else CONVERT(datetime,T.c.value('./dateend[1]', 'varchar(20)'),103) end AS DATEEND
  into #temp_f033
  FROM   @Xml.nodes('/packet/zap') T(c)

UPDATE t1 
SET 
      [NAM_SPMO] = t2.NAM_SPMO
      ,[NAM_SK_SPMO] = t2.NAM_SK_SPMO
      ,[PHONE] = t2.PHONE
      ,[OSP] = t2.OSP
      ,[VID_SPMO] = t2.VID_SPMO
      ,[OID_SPMO] = t2.OID_SPMO
      ,[DATEEND] = t2.DATEEND
FROM [IES].[T_F033_SPMO] t1
join #temp_f033 t2 on t2.IDSPMO = t1.IDSPMO and t2.DATEBEG = t1.DATEBEG

INSERT INTO [IES].[T_F033_SPMO]
			(F033SpmoID, IDSPMO, NAM_SPMO, NAM_SK_SPMO, PHONE, OSP, VID_SPMO, OID_SPMO, DATEBEG, DATEEND)
	select
			NEWID(), t1.IDSPMO, t1.NAM_SPMO, t1.NAM_SK_SPMO, t1.PHONE, t1.OSP, t1.VID_SPMO, t1.OID_SPMO, t1.DATEBEG, t1.DATEEND
FROM #temp_f033 t1
left join [IES].[T_F033_SPMO] t2 on t2.IDSPMO = t1.IDSPMO and t2.DATEBEG = t1.DATEBEG
where t2.F033SpmoID is null

drop table #temp_f033

END