Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask DOT NET Expert

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

DOT NET, Programming

  • Category:- DOT NET
  • Reference No.:- M9508957

Have any Question?


Related Questions in DOT NET

Scenariouser modelling inc would like to organize a series

Scenario User Modelling Inc. would like to organize a series of conferences focusing on research topics in the area of user adaptive systems and personalization. They need to organize annual conferences for researchers a ...

Assignment specificationthis assignment is split into two

ASSIGNMENT SPECIFICATION This assignment is split into two documents. 1. Assignment Specification. This document contains the specification for what you will be required to do. 2. Assignment Addendum. This document conta ...

Summarythis assessment requires you to develop a simple

Summary This assessment requires you to develop a simple text editor with a basic offline user login. It is a windows application that will allow new uses to be added, existing users to login, and to create/save/edit tex ...

Casewe sell pdfs from our site and pdfs can be ordered from

Case We sell pdf''s from our site and pdfs can be ordered from the site by selecting a book, putting it in a shopping cart, then signing in with a password or as a guest, and then checking out. For some reason, when a pu ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As