How to do error handling in SQL