Sie sind auf Seite 1von 4

3/23/13

Using RAISERROR

Using RAISERROR
SQL Server 2008 R2
4 out of 8 rated this helpful RAISERROR is used to return messages back to applications using the same format as a system error or warning message generated by the SQL Server Database Engine. RAISERROR can return either: A user-defined error message that has been created using the sp_addmessage system stored procedure. These are messages with a message number greater than 50000 that can be viewed in the sys.messages catalog view. A message string specified in the RAISERROR statement. RAISERROR can also: Assign a specific error number, severity, and state. Request that the error be logged in the Database Engine error log and the Microsoft Windows application log. Substitute argument values into the message text, much like the C language printf_s function. Both RAISERROR and PRINT can be used to return informational or warning messages to an application. The message text returned by RAISERROR can be built using string substitution functionality similar to the printf_s function of the C standard library, whereas PRINT can only return a character string or character expression. A RAISERROR severity of 11 to 19 executed in the TRY block of a TRYCATCH construct causes control to transfer to the associated CATCH block. Specify a severity of 10 or lower to return messages using RAISERROR without invoking a CATCH block. PRINT does not transfer control to a CATCH block. When RAISERROR is used with the msg_id of a user-defined message in sys.messages, msg_id is returned as the SQL Server error number, or native error code. When RAISERROR is used with a msg_str instead of a msg_id , the SQL Server error number and native error number returned is 50000. When you use RAISERROR to return a user-defined error message, use a different state number in each RAISERROR that references that error.
msdn.microsoft.com/en-IN/library/ms177497(v=sql.105).aspx 1/4

3/23/13

Using RAISERROR

This can help in diagnosing the errors when they are raised. Use RAISERROR to: Help in troubleshooting Transact-SQL code. Check the values of data. Return messages that contain variable text. Cause execution to jump from a TRY block to the associated CATCH block. Return error information from the CATCH block to the calling batch or application. The following example substitutes the values from the D B _ I D ( )and D B _ N A M E ( )functions in a message sent back to the application:

D E C L A R E@ D B I DI N T ; S E T@ D B I D=D B _ I D ( ) ; D E C L A R E@ D B N A M EN V A R C H A R ( 1 2 8 ) ; S E T@ D B N A M E=D B _ N A M E ( ) ; R A I S E R R O R ( N ' T h ec u r r e n td a t a b a s eI Di s : % d ,t h ed a t a b a s en a m ei s :% s . ' , 1 0 ,-S e v e r i t y . 1 ,-S t a t e . @ D B I D ,-F i r s ts u b s t i t u t i o na r g u m e n t . @ D B N A M E ) ;-S e c o n ds u b s t i t u t i o na r g u m e n t . G O

This example provides the same information using a user-defined message.

E X E C U T Es p _ d r o p m e s s a g e5 0 0 0 5 ; G O E X E C U T Es p _ a d d m e s s a g e5 0 0 0 5 ,-M e s s a g ei dn u m b e r . 1 0 ,-S e v e r i t y . N ' T h ec u r r e n td a t a b a s eI Di s :% d ,t h ed a t a b a s en a m ei s :% s . ' ; G O D E C L A R E@ D B I DI N T ; S E T@ D B I D=D B _ I D ( ) ;


msdn.microsoft.com/en-IN/library/ms177497(v=sql.105).aspx 2/4

3/23/13

Using RAISERROR

D E C L A R E@ D B N A M EN V A R C H A R ( 1 2 8 ) ; S E T@ D B N A M E=D B _ N A M E ( ) ; R A I S E R R O R( 5 0 0 0 5 , 1 0 ,-S e v e r i t y . 1 ,-S t a t e . @ D B I D ,-F i r s ts u b s t i t u t i o na r g u m e n t . @ D B N A M E ) ;-S e c o n ds u b s t i t u t i o na r g u m e n t . G O

The following code example shows how to use RAISERROR inside a TRY block to cause execution to jump to the associated CATCH block. It also shows how to use RAISERROR to return information about the error that invoked a CATCH block. Note RAISERROR can generate errors with state from 1 through 127 only. Because the Database Engine may raise errors with state 0, we recommend that you check the error state returned by ERROR_STATE before passing it as a value to the state parameter of RAISERROR.

B E G I NT R Y -R A I S E R R O Rw i t hs e v e r i t y1 1 1 9w i l lc a u s ee x e c u t i o nt o -j u m pt ot h eC A T C Hb l o c k R A I S E R R O R( ' E r r o rr a i s e di nT R Yb l o c k . ' , -M e s s a g et e x t . 1 6 ,-S e v e r i t y . 1-S t a t e . ) ; E N DT R Y B E G I NC A T C H D E C L A R E@ E r r o r M e s s a g eN V A R C H A R ( 4 0 0 0 ) ; D E C L A R E@ E r r o r S e v e r i t yI N T ; D E C L A R E@ E r r o r S t a t eI N T ; S E L E C T@ E r r o r M e s s a g e=E R R O R _ M E S S A G E ( ) , @ E r r o r S e v e r i t y=E R R O R _ S E V E R I T Y ( ) , @ E r r o r S t a t e=E R R O R _ S T A T E ( ) ; -U s eR A I S E R R O Ri n s i d et h eC A T C Hb l o c kt or e t u r n -e r r o ri n f o r m a t i o na b o u tt h eo r i g i n a le r r o rt h a t -c a u s e de x e c u t i o nt oj u m pt ot h eC A T C H b l o c k . R A I S E R R O R( @ E r r o r M e s s a g e ,-M e s s a g et e x t . @ E r r o r S e v e r i t y ,-S e v e r i t y . @ E r r o r S t a t e-S t a t e .
msdn.microsoft.com/en-IN/library/ms177497(v=sql.105).aspx 3/4

3/23/13

Using RAISERROR

@ E r r o r S t a t e-S t a t e . ) ;

E N DC A T C H ;

See Also
Reference
RAISERROR (Transact-SQL) ERROR_LINE (Transact-SQL) ERROR_MESSAGE (Transact-SQL) ERROR_NUMBER (Transact-SQL) ERROR_PROCEDURE (Transact-SQL) ERROR_SEVERITY (Transact-SQL) ERROR_STATE (Transact-SQL)

Concepts

Using @@ERROR Using PRINT Using TRY...CATCH in Transact-SQL Handling Errors and Messages in Applications Handling Errors in Server-to-Server Remote Stored Procedures

Community Additions
2013 Microsoft. All rights reserved.

msdn.microsoft.com/en-IN/library/ms177497(v=sql.105).aspx

4/4

Das könnte Ihnen auch gefallen