do $$
begin
	if exists 
	(
		select * from pg_proc p
		where p.proname = 'sp_f006videxp'
	)
	then
		drop function public.sp_f006videxp(xml, varchar);
	end if;
end;
$$ language plpgsql;
CREATE or Replace Function public.sp_f006videxp
(
 "F006" xml,
 "FileName" varchar(100)
)
RETURNS void As 
$$
BEGIN



Drop Table If Exists TmpF006;
Create Temp Table TmpF006 as

      
          
      	select
       
       
nullif(trim(xpath('//IDVID/text()', T)::text,'{}"'),'')::int as IDIDST,
nullif(trim(xpath('//VIDNAME/text()', T)::text,'{}"'),'')::varchar(2000) as STNAME,
nullif(trim(xpath('//DATEBEG/text()', T)::text,'{}"'),'')::date as DATEBEG,
nullif(trim(xpath('//DATEEND/text()', T)::text,'{}"'),'')::date as DATEEND

from unnest(xpath('packet/zap', "F006")) as tt;

update IES.T_F006_CONTROL_TYPE 
	set  t2.VIDNAME=t1.VIDNAME
		,t2.DATEEND=t1.DATEEND
	from TmpF006 t1
	where t1.IDVID=t2.IDVID;

	insert into IES.T_F006_CONTROL_TYPE(IDVID,VIDNAME,DATEBEG,DATEEND,F006ControlTypeID)
	select t1.IDVID,t1.VIDNAME,t1.DATEBEG,t1.DATEEND,uuid_generate_v1()
	from TmpF006 t1
	left join IES.T_F006_CONTROL_TYPE t2 on (t1.IDVID=t2.IDVID)
	where t2.IDVID is null;
		
	
	
	
	drop table if exists TmpF006;
	END;
$$ Language plpgsql;