Things can go wrong in sql operation. So how we handle Errors in sql server.
We can detect errors in two ways
- Try/catch
- @@ERROR
@@ERROR
If there is no errors based on the last operation @@ERROR will be set to 0 . If something went wrong it will be set to value other than 0. How ever this not best way to handle errors in sql server.
TRY/CATCH
BEGIN TRY -- this is where we put our codes that can be generate errors END TRY BEGIN CATCH -- this where we put code to handle errors END CATCH
Big thing in TRY block is always we’re going to assume success because only way that we’re going to get to the next line of code is if the line before is succeeded