Fixed sql issues