SQL Server will raise errors when the code you have written cannot or should not execute.
For example, a table should not be created if one with the same name already exists. Suppose you have a stored procedure named UpdateOneRecord which changes one record at a time. The logic of this stored procedure will allow you to potentially update two records with the same info. If it was against company policy to update more than one record at a time, it’s extremely unlikely that anyone would ever attempt to update multiple records at once.
However, because SQL Server has no restriction against updating one or many records in one transaction, you want to add a layer of protection to help enforce company policy.
This is a case where you don’t want SQL Server to allow this update, even though SQL Server doesn’t define it as an error. To accomplish this goal, you can raise your own error message based on conditions which you define.
RAISERROR generates an error message and initiates error processing for the session. RAISERROR can either reference a user-defined message stored in the sys.messages catalog view or build a message dynamically. The message is returned as a server error message to the calling application or to an associated CATCH block of a TRY/CATCH construct.
Thanks for reading this article,
Next steps :
- Share this with your colleagues because Sharing is Learning
- Comment below if you need any assistance