Improved SQL error logging