Fixing broken sql