Re-orgnize code to fix issue that when no sql found then the rep search also be abort.