Fixed sql bug