/********************************
      a55
********************************/
/* t01->a55 */
INSERT INTO a55 
(nr01, p01, p02, p03, p08, p06, p010, p011, 
remfdate, remftime, remfuser, remdate, remtime, remuser)
SELECT t01.p302 as nr01, 
       t01.p02 as p01, 
       t01.p02t as p02, 
       ifnull(a01.nr01, 0) as p03, 
       1 as p08, 
       '*#*' as p06, 
       t01.p05 as p010, 
       t01.p06 as p011, 
       t01.remfdate as remfdate, 
       t01.remftime as remftime, 
       t01.remfuser as remfuser, 
       curdate()    as remdate, 
       curtime()    as remtime, 
       '{user_pswd}' as remuser 
FROM t01
LEFT JOIN a55 ON a55.nr01=t01.p302
LEFT JOIN a01 ON a01.P21=t01.p01
where t01.p303 IN (2, 4)
   && a55.nr01 is null
GROUP BY t01.p302;   
/*peg_end*/


/* i01->a55 */
INSERT INTO a55 
(nr01, p01, p02, p03, p08, p06, 
remfdate, remftime, remfuser, remdate, remtime, remuser)
SELECT i01.p06 as nr01, 
       i01.p013 as p01, 
       i01.p013t as p02,
       ifnull(a01.nr01, 0) as p03, 
       1 as p08, 
       '*#*' as p06, 
       i01.remfdate as remfdate, 
       i01.remftime as remftime, 
       i01.remfuser as remfuser, 
       curdate()    as remdate, 
       curtime()    as remtime, 
       '{user_pswd}' as remuser 
FROM i01
LEFT JOIN a55 ON a55.nr01=i01.p06
LEFT JOIN a01 ON a01.p21=i01.p014
where i01.p07=2
   && a55.nr01 is null
GROUP BY i01.p06;   
/*peg_end*/
