/********************************
      a56
********************************/
/* t01->a56 */
INSERT INTO a56 
(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(a00.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 a56 ON a56.nr01=t01.p302
LEFT JOIN a00 ON a00.P21=t01.p01
where t01.p303 IN (1, 3)
   && a56.nr01 is null
GROUP BY t01.p302;   
/*peg_end*/


/* i01->a56 */
INSERT INTO a56 
(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(a00.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 a56 ON a56.nr01=i01.p06
LEFT JOIN a00 ON a00.p21=i01.p014
where i01.p07=1
   && a56.nr01 is null
GROUP BY i01.p06;   
/*peg_end*/