delete from d00;
/*peg_end*/

/***********************
    a00=> d00
***********************/    
SET @n1:=ifnull((SELECT max(nr01) from d00), 0) ;
/*peg_end*/

update a00 set pnr01=@n1:=@n1+1;
/*peg_end*/

insert into d00    
(nr01, p19, p2, p3, p4, p23, p5, p7_1, p20, p22, pc22,
p16, p8, p9, p10, p11, p803, p022, p900, 
remfdate, remftime, remfuser, remdate, remtime, remuser)    

select * from
(select    
    a00.pnr01 as nr01,    
    a00.p19 as p19,    
    a00.p2  as p2,    
    a00.p3  as p3,    
    a00.p4  as p4,    
    a00.p23 as p23,    
    a00.p5 as p5,    
    a00.p7 as p7_1, 
    a00.p20 as p20,    
    a00.p22 as p22,    
    a00.pc22 as pc22,    
    2 as p16,    
    a00.p8 as p8,    
    a00.p9 as p9,    
    a00.p10 as p10,    
    a00.p01 as p11,    
    a00.p11 as p803,    
    curdate() as p022,    
    a00.p51 as p900, 
    remfdate, remftime, remfuser, remdate, remtime, remuser        
from a00) as tbl01;
/*peg_end*/


/***********************
    a01=> d00
***********************/    
SET @n1:=ifnull((SELECT max(nr01) from d00), 0) ;
/*peg_end*/

update a01 set pnr01=@n1:=@n1+1;
/*peg_end*/

insert into d00    
(nr01, p19, p2, p3, p4, p23, p5, p7_1, p20, p22, pc22,    
p16, p8, p9, p10, p11, p803, p022, p900, 
remfdate, remftime, remfuser, remdate, remtime, remuser)    
select * from
(
select    
    a01.pnr01 as nr01,    
    a01.p19 as p19,    
    a01.p2  as p2,    
    a01.p3  as p3,    
    a01.p4  as p4,    
    a01.p23 as p23,    
    a01.p5 as p5,    
    a01.p7 as p7_1, 
    a01.p20 as p20,    
    a01.p22 as p22,    
    a01.pc22 as pc22,    
    1 as p16,    
    a01.p8 as p8,    
    a01.p9 as p9,    
    a01.p10 as p10,    
    a01.p01 as p11,    
    a01.p11 as p803,    
    curdate() as p022,    
    a01.p51 as p900, 
remfdate, remftime, remfuser, remdate, remtime, remuser        
from a01) as tbl01;
/*peg_end*/

/***************************
       d00 => d64
***************************/       
delete from d64;
/*peg_end*/

SET @n1:=ifnull((SELECT max(nr01) from d64), 0) ;
/*peg_end*/

insert into d64    
(nr01, p00, p001, p01, p02, p05, p06, pd100, 
remfdate, remftime, remfuser, remdate, remtime, remuser) 
select    
    @n1:=@n1+1 as nr01,    
    d00.nr01 as p00,    
    d00.p8 as p001,    
    d00.p8 as p01,    
    1 as p02,    
    1 as p05,    
    1 as p06,    
    'phone-01' as pd100, 
    remfdate, remftime, remfuser, remdate, remtime, remuser    
from d00    
WHERE d00.p8<>''    

UNION ALL    

select    
    @n1:=@n1+1 as nr01,    
    d00.nr01 as p00,    
    d00.p9 as p001,    
    d00.p9 as p01,    
    1 as p02,    
    1 as p05,    
    2 as p06,   
    'phone-02' as pd100, 
    remfdate, remftime, remfuser, remdate, remtime, remuser    
from d00    
WHERE d00.p9<>''    

UNION ALL   

select    
    @n1:=@n1+1 as nr01,    
    d00.nr01 as p00,    
    d00.p10 as p001,    
    d00.p10 as p01,    
    2 as p02,    
    1 as p05,    
    3 as p06,    
    'fax-01' as pd100, 
    remfdate, remftime, remfuser, remdate, remtime, remuser    
from d00    
WHERE d00.p10<>'';
/*peg_end*/

/***************************
       d00 => d65
***************************/       
delete from d65;
/*peg_end*/

SET @n1:=ifnull((SELECT max(nr01) from d65), 0) ;
/*peg_end*/

insert into d65    
(nr01, p00, p01, p02, p05, pd100, 
remfdate, remftime, remfuser, remdate, remtime, remuser) 
select    
    @n1:=@n1+1 as nr01,    
    d00.nr01 as p00,    
    d00.p11 as p11,    
    1 as p02,    
    1 as p05,    
    'email-01' as pd100, 
    remfdate, remftime, remfuser, remdate, remtime, remuser    
from d00    
WHERE d00.p11<>'';
/*peg_end*/
