bug fix when assembling sql statement