/***********************
   update d40
***********************/
update d40, d40_std
set d40.p01=d40_std.p01, 
    d40.remdate=curdate(), 
    d40.remtime=curtime(), 
    d40.remuser='{user_pswd}'
where d40.p02=d40_std.p02
   && d40.p01<>d40_std.p01;
/*peg_end*/

/***********************
   insert into d40
***********************/
insert into d40
(nr01, p01, p02, 
remfdate, remftime, remfuser, remdate, remtime, remuser) 
select peg_new_nr01('d40', 1) as nr01, 
       d40_std.p01 as p01, 
       d40_std.p02 as p02, 
       curdate() as remfdate, 
       curtime() as remftime, 
       '{user_pswd}' as remfuser,
       curdate() as remdate, 
       curtime() as remtime, 
       '{user_pswd}' as remuser
from d40_std
left join d40 ON d40.p02=d40_std.p02
where d40.nr01 is null;
/*peg_end*/
       
   
/***********************
   mark to delete
***********************/
update d40
LEFT JOIN d40_std ON d40.p02=d40_std.p02
set d40.p02='',
    d40.remdate=curdate(), 
    d40.remtime=curtime(), 
    d40.remuser='{user_pswd}'
where d40_std.p02 is null;
/*peg_end*/

drop table if exists d40_std; 
/*peg_end*/
