Fix invalid SQL issue