Fix some SQL issues