I'm not sure I understand your question 100% correct, but try this:insert into iteminfo (itmcode ,itmhead ,itmlevel ) select max(ifnull(b.itmcode,a.itmcode*power(10,a.itmlevel)))+1 ,max(a.itmcode) ,max(a.itmlevel)+1 from iteminfo as a left outer join iteminfo as b on b.itmhead=a.itmcode and b.itmlevel=a.itmlevel+1 where a.itmcode=1101 and a.itmlevel=3 group by a.id
The above code assumes all fields are numeric.ps.: there might be a syntax error (or several), as I currently don't have access to a SQL server - long live notepad :-D