Fixing issue with invalid SQL