update erpa10, erpa10 as erpa10_1
SET erpa10.p02=erpa10_1.p02, 
    erpa10.p03=erpa10_1.p03, 
    erpa10.remdate=curdate(), 
    erpa10.remtime=curtime(), 
    erpa10.remuser='{user_pswd}'
WHERE erpa10_1.p00='{peg_str}'
   && erpa10.p00='{nt_p01}'
   && erpa10.p01=erpa10_1.p01
   && (    erpa10.p02<>erpa10_1.p02
        || erpa10.p03<>erpa10_1.p03
      );
/*peg_end*/

INSERT INTO erpa10
(nr01, p00, p01, p02, p03, 
remfdate, remftime, remfuser, remdate, remtime, remuser)
SELECT peg_new_nr01('erpa10', 1) as nr01, 
       '{nt_p01}' as p00, 
       erpa10.p01 as p01, 
       erpa10.p02 as p02, 
       erpa10.p03 as p03, 
       
       curdate() as remfdate,
       curtime() as remftime,
       '{user_pswd}' as remfuser, 
       curdate() as remdate,
       curtime() as remtime,
       '{user_pswd}' as remuser
FROM erpa10        
LEFT JOIN erpa10 as erpa10_1 ON erpa10_1.p00='{nt_p01}'
                             && erpa10_1.p01=erpa10.p01
WHERE erpa10.p00='{peg_str}'
   && erpa10_1.nr01 is null;
/*peg_end*/
