I need to pass the error to the application. I wrote the stored procedures as below:
Does it pass the error to the application or we need to do the something more?
Eg.
create procedure insert_emp
as
begin try
begin tran
insert into emp values(..........)
commit;
end try
begin catch
if @@trancount > 0
rollback
declare @errMessage varchar(2000),
declare @errNumber int
select @errMessage = error_message(), @errNumber = error_number()
raiserror(@errMessage, @errNumber,1)
end catch
If I need to pass 0 if there is no error and a error number if there was a error, how to do it using raiserror? How can we log the error to error_table using raiserror? More importantly can above eg. pass the error to an application?
Don’t pass
raiserror(@errMessage, @errNumber,1). The arguments for RAISERROR are message id, severity, state. You are passing @errNumber for severity, which is incorrect.You should instead raise a custom error and pass the caught exception information in the error message as message format inserts:
You should use severity 16 for errors that you want to throw a SqlException in the client side processing (or other exception, according to the client access technology used).
You should use severity 1 for informational messages.