Managing Custom Errors in SQL Server

Creating Custom Errors in SQL Server is not a hard thing to do, but managing those errors across multiple developers can be difficult. The question becomes: who has already used what custom error? This often leads to hours of wasted time. To make things easier, centralize all of your error messages in a stored procedure.

The first thing you need to do is create a stored procedure that will add the message to the sysmessages table. Because you will be calling this often, it needs to check to see if the message already exists and if not, add it.

 CREATE procedure dbo.CreateCustomError	(	@ErrNumber	int,	@ErrMessage	Varchar(2000)AsSET NOCOUNT ONDeclare @ErrorNumber bigintSet @ErrorNumber = dbo.vbObjectError(@ErrNumber);if not exists (select Error from Master..sysmessages _where error [email protected])	exec dbo.sp_addmessage @msgnum = @ErrorNumber, [email protected] = 10,		@msgtext = @ErrMessage,		@with_log = 'false';GO

Now, just create the following stored procedure to manage the addition of these messages for your developers.

 CREATE procedure dbo.InitiateAllErrorsAsSET NOCOUNT ON---------------------------Joe Smith Security Errors6000-6299---------------------------------------- exec dbo.CreateCustomError 6001, 'Security _Failure - User Can not performaction on specified on whatever';---------------------------Bob Doe Errors6300-6599---------------------------------------- exec dbo.CreateCustomError 6300, 'Delete whatver _Error - The whateveralready exists.';GO

From Query Analyzer, just run the above stored procedure whenever new custom errors are added. You don’t need to worry about one developer’s custom errors getting written over another developer’s custom error.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job