insert into d99102
(nr01, p00, d00, p01, dt, tm, p300, tbl, tbl_nr01, 
remfdate, remftime, remfuser, remdate, remtime, remuser)

SELECT peg_new_nr01('d99102', 1) as nr01, 
       tbl01.p00  as p00, 
       tbl01.d00  as d00, 
       tbl01.p01  as p01, 
       tbl01.dt   as dt, 
       tbl01.tm   as tm, 
       tbl01.p300 as p300, 
       tbl01.tbl  as tbl, 
       tbl01.tbl_nr01 as tbl_nr01, 
       
       curdate() as remfdate, 
       curtime() as remftime, 
       '{user_pswd}' as remfuser, 
       curdate() as remdate, 
       curtime() as remtime, 
       '{user_pswd}' as remuser
FROM 
(        
       
          select 'sms' as p00, 
                 d65.p00 as d00, 
                 concat(d65.p01, ' **auto**') as p01, 
                 curdate() as dt, 
                 curtime() as tm, 
                 d65.p300 as p300, 
                 'd65' as tbl, 
                 d65.nr01 as tbl_nr01,
                 
                 ifnull( (select d99102_in.p300
                          from d99102 as d99102_in
                          where d99102_in.tbl_nr01=d65.nr01
                             && d99102_in.tbl='d65'
                          order by d99102_in.dt desc, d99102_in.tm desc
                          limit 1), 0) as dp300
          from d65                 
) as tbl01
WHERE tbl01.p300<>tbl01.dp300;                              
          