add the replace temp with query
refactor replace temp with query