Raise custom errors from stored procedure, DOT NET Programming

Assignment Help:

How can you raise custom errors from stored procedure?

The RAISERROR statements are  used to produce an ad hoc error message or to retrieve acustom message which is stored in the sysmessages table. You can use this statement with the error handling code presented in the previous part to implement custom error messages in your applications. The syntax of the statement is shown below.

RAISERROR ({msg_id |msg_str }{,severity ,state }

[ ,argument [ ,,...n ] ] ))

[ WITH option [ ,,...n ] ]

The description of the components of the statement is as shown below:

msg_id :-This ID is used for an error message,that is stored in the error column in the sysmessages.

msg_str :-This shows a custom message that is not contained in sysmessages.

severity :- The severity level is associated with the error. The valid values are at 0-25.The Severity levels 0-18 can be used by any user, but 19-25 are only available to those members of the fixed-server role sysadmin. When the levels 19-25 are used, then the WITH LOG option is required.

The state A value that shows the invocation state of the error. The valid values are 0-127. That  value is not used by the SQL Server.

Argument, . . .

It is used by One or more variables that are used to customize the message. For e.g., you could pass the current process ID (@@SPID) so it could be displayed in the message.

WITH option, . . .

The three values that are used with this optional argument are described here.

LOG - It Forces the error to log in the SQL Server error log and the NT application log.

NOWAIT - This Sends the message immediately to the client.

SETERROR - This Sets @@ERROR to the unique ID for the message or 50,000.

The number of options available for the statement made it seem complicated, but it is actually simple to use. The following shows how to create an ad hoc message with the severity of 10 and a state of 1.

RAISERROR ('An error occurred during updating the NonFatal table',10,1)

--Results--

An error definitely occurred on updating the NonFatal table.The statement does not have to be used in the conjunction with any other code, but for our purposes it will be used with the error handling code presented earlier. The shown below alters the ps_NonFatal_INSERT procedure to use RAISERROR.

USE tempdb go

ALTER PROCEDURE ps_NonFatal_INSERT

@Column2 int =NULL AS

DECLARE @ErrorMsgID int

INSERT NonFatal VALUES (@Column2) SET @ErrorMsgID =@@ERROR

IF @ErrorMsgID <>0

BEGIN

RAISERROR ('An error occured updating the NonFatal table',10,1) END


Related Discussions:- Raise custom errors from stored procedure

Print pyramids of numbers - c# program, Print Pyramids of Numbers - C# Prog...

Print Pyramids of Numbers - C# Program Hello can you please provide me some examples regarding to the Print Pyramids of Numbers in C#.

Explain windows authentication schemes, Explain Windows authentication Sche...

Explain Windows authentication Schemes There are four parts of windows authentication I. Integrated Windows authentication II. Basic and basic with SSL authentication

What is a parameter, What is a parameter? Explain the new types of paramete...

What is a parameter? Explain the new types of parameters introduced in C# 4.0. A parameter is a special type of variable, which is used in a function to give a piece of informa

Investment equation - c# program, Investment Equation - C# Program I di...

Investment Equation - C# Program I didn't know the exact code for computing Investment Equation. Can anybody suggest me the correct way for my problem?

We need asp.net expert with simple html, We need Asp.net expert with simple...

We need Asp.net expert with simple html I am in search of a long term asp.net developer to build as well as maintain a volleyball player management website. We would like to inc

What are abstract classes, What are abstract classes? What are the distinct...

What are abstract classes? What are the distinct characteristics of an abstract class? An abstract class is a class that cannot be instantiated and is always used as a base cla

In what instances you will declare constructor to be private, When we creat...

When we create a private constructor, we can not create object of the class directly from the client. So you will use the private constructors whenever you do not want instances of

I need a custom project - c# developer, Project Description: My project ...

Project Description: My project is to construct for me an application that emulates the http requests made in a webapp. It will need a lot of dedication and skill in C#,.net,

Use of resource manager class, What's the use of resource manager class? ...

What's the use of resource manager class? The ResourceManager class helps us to read the resource files and get the values by using key. At First you require to create the obje

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd