Fix misplaced SQL statement