/******************
    d64
******************/    

SET @n1:=ifnull( (select max(nr01) from d64 where nr01>=9000000000), 9000000000);
/*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, 
       curdate() as remfdate, 
       curtime() as remftime, 
       '*' as remfuser, 
       curdate() as remdate, 
       curtime() as remtime, 
       '*' as remuser
FROM d00 
LEFT JOIN d64 ON d64.p00=d00.nr01
              && d64.pd100='phone-01' 
WHERE d00.p8<>''       
   && d64.nr01 is null

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, 
       curdate() as remfdate, 
       curtime() as remftime, 
       '*' as remfuser, 
       curdate() as remdate, 
       curtime() as remtime, 
       '*' as remuser
FROM d00 
LEFT JOIN d64 ON d64.p00=d00.nr01
              && d64.pd100='phone-02' 
WHERE d00.p9<>''       
   && d64.nr01 is null

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, 
       curdate() as remfdate, 
       curtime() as remftime, 
       '*' as remfuser, 
       curdate() as remdate, 
       curtime() as remtime, 
       '*' as remuser
FROM d00 
LEFT JOIN d64 ON d64.p00=d00.nr01
              && d64.pd100='fax-01' 
WHERE d00.p10<>''       
   && d64.nr01 is null;
/*peg_end*/ 

/******************
    d65
******************/    
SET @n1:=ifnull( (select max(nr01) from d65 where nr01>=9000000000), 9000000000);
/*peg_end*/

INSERT INTO d65 
(nr01, p00, p01, p05, pd100, 
remfdate, remftime, remfuser, remdate, remtime, remuser) 

SELECT @n1:=@n1 + 1 as nr01, 
       d00.nr01 as p00, 
       d00.p11 as p01, 
       1 as p05, 
       'email-01' as pd100, 
       curdate() as remfdate, 
       curtime() as remftime, 
       '*' as remfuser, 
       curdate() as remdate, 
       curtime() as remtime, 
       '*' as remuser
FROM d00 
LEFT JOIN d65 ON d65.p00=d00.nr01
              && d65.pd100='email-01' 
WHERE d00.p11<>''       
   && d65.nr01 is null;
/*peg_end*/
   


   