Error Escalation In SQL Server:
What is Error Escalation?
In DBMS, error could stand on multiple levels, let say database storage level(file path missing, change in disk, corrupt files etc), server connectivity error ( wrong instance connection, unauthorised access error, restricted users activity error), table level or data level error( data truncation, mismatch data type, out of storage etc).
These are some common error which by default occurs and must be rectified after receiving it. But some database error that are cause by the queries written on the database could be recognised before execution and proper solution can be created in order to get rid of the error in the runtime of query.
Let's understand the topic with an example:
If say, the dynamic query is written for checking the existence of any object where object name is the user parameter, in such case may be user provide an input with symbols or keyboard characters which can cause the error, so here we will see how error escalation will help in get rid of user inputs for error.
Declare @user_input varchar(25)
Declare @query varchar (max)
Set @query='select * from sys.objects where name ='+@user_input
Exec(@query)
The above query runs very fine if the user give correct parameters but let suppose user entered the value as:
Set @user_input= ;[abcd];go
Now here 'go' will be treated as a keyword in SQL as well as semi colon will be treated as special character to end the statement , so here error is escalated by the user which on runtime can generate enough logs for error as well as use multiple resources to do so.
To get rid of the error one can use Try.. catch block and tell user to pass correct parameter value.
Begin try
Declare @user_input varchar(25)
Set @user_input= ;[abcd];go
If @user_input like ';%' or @user_input like ' ' or @user_input like '[€¥$√π^¢%]
Throw 51000, 'wrong input please follow the input value rules,1;
End try
Begin catch
Print 'pass the value again'
End catch
With such art, we can save error to be escalated.
Thankyou, for any queries please post in comments.

.jpeg)
Comments
Post a Comment