How to throw an exception in sqlserver function or stored procedure.

raiserror role: raiserror is used to throw an error. [ The following information comes from the help of sql server 2005 ]  

The syntax is as follows: 

RAISERROR ({msg_id | msg_str |  @local_variable }         
            {, Severity, State}         
            [ , argument [, ... the n- ] ] 
          )        
   [ the WITH the Option [, .. .n ] ] 

described briefly: 

first parameter: {msg_id | msg_str |  @local_variable } 
      msg_id: indicates a message may be defined in the code table sys.messages;  
              use sp_addmessage store user-defined directory in view sys.messages error message number.
              error number is user-defined error message should be greater than 50,000 .

     msg_str: represents may be a user-defined message, the error message can be up to 2047 characters; 
             (if constant, use N ' XXXX ' , as is the nvarchar) 
              when specifying msg_str, the RAISERROR raises an error number for the 5000 error message. 

     @local_variable : represents may be formatted according to the embodiment of msg_str string variable. 
            
The second parameter: severity 
            severity level associated with the user-defined message. (This is very important) 
            Any user can specify 0 Dao 18 Severity levels.
            [ 0,10 ] in the closed interval, the catch will not jump; 
            if [ 11,19 ]  , then skip to the catch;
            If [ 20, infinite), the process directly terminates the database connection; 

third parameter: state 
            if the same user-defined error caused in a plurality of positions, 
            using only state numbers help identify code segments causing the error for each location. 
          
            Is any integer between 1-127. (Default is state 1) 
            when the error is generated when the state value is 127 or greater than 0! 

The fourth parameter: argument 
            used to replace or correspond to msg_str msg_id parameter variables defined in the message. 

The fifth parameter: option 
            error customization options, the table may be any value: 
            the LOG: records the error in the error logs, and application logs; 
            the NOWAIT: immediately sends a message to the client; 
            setError: The @@ ERROR value is set to the value and ERROR_NUMBER msg_id or 50000; 
 
  [the SQL ] Code example 

- example @raiseErrorCode nvarchar 1DECLARE (50) 
the sET  @raiseErrorCode  =  the CONVERT ( nvarchar ( 50 ), YOURUNIQUEIDENTIFIER KEY)
RAISERROR('%s INVALID ID. There is no record in table',16,1, @raiseErrorCode)

--示例2RAISERROR (
             N'This is message %s %d.', -- Message text,
             10,                        -- Severity,
             1,                         -- State,
             N'number',                 -- First argument.
             5                          -- Second argument.
          ); 
-- The message text returned is: This is message number 5.
GO

--示例3RAISERROR (N'<<%*.*s>>', -- Message text.
           10,           -- Severity,
           1,            -- State,
           7,            -- First argument used for width.
           3,            -- Second argument used for precision.
           N'abcde');    -- Third argument supplies the string.
-- The message text returned is: <<    abc>>.
GO

--Example 4RAISERROR (N '<< >> 7.3s%', - the Message text. 
           10 ,            - Severity, 
           . 1 ,             - State, 
           N ' ABCDE ' );     - . The First String argument Supplies 
- of The Message text iS returned: ABC << >>. 
the GO 

- example. 5     
- A. returns an error message from the CATCH block 
the following example shows how to use the code in the TRY block RAISERROR cause execution to jump to the associated CATCH block in. 
It also shows how to use RAISERROR to return information about invoking the CATCH block of error. 

The BEGIN TRY
     the RAISERROR ( ' Error Block TRY raised in. ', -- Message text.
                16, -- Severity.
                1 -- State.
               );
END TRY
BEGIN CATCH
    DECLARE @ErrorMessage NVARCHAR(4000);
    DECLARE @ErrorSeverity INT;
    DECLARE @ErrorState INT;

    SELECT 
        @ErrorMessage = ERROR_MESSAGE(),
        @ErrorSeverity = ERROR_SEVERITY(),
        @ErrorState = ERROR_STATE();

    RAISERROR (@ErrorMessage ,   - the Message text. 
               @ErrorSeverity , - Severity. 
               @ErrorState      - State. 
               );
 The END the CATCH; 

- Example. 6 
- B. create ad hoc message sys.messages the 
following example shows how initiator sys.messages message stored in the catalog views. 
The message stored by the system sp_addmessage procedure to add a message to the number 50005 sys.messages catalog view. 

the sp_addmessage @msgnum  =  50005 ,
                @severity  =  10 ,
                @msgtext  = N ' << >> 7.3s% ' ;
 the GO 

the RAISERROR (50005 , - . The Message ID 
           10 ,     - Severity, 
           . 1 ,      - State, 
           N ' ABCDE ' ); - First Supplies The String argument. 
- of The Message text returned IS: ABC << >>. 
The GO 

sp_dropmessage on @ , MSGNUM  =  50005 ;
 the GO 

- example. 7 
- C. local variable provides the message text 
the following sample code shows how to use local variables RAISERROR statement provides the message text. the sp_addmessage @msgnum  =  50005 ,
               @severity  =  10,
              @msgtext = N'<<%7.3s>>';
GO

RAISERROR (50005, -- Message id.
           10,    -- Severity,
           1,     -- State,
           N'abcde'); -- First argument supplies the string.
-- The message text returned is: <<    abc>>.
GO

sp_dropmessage @msgnum = 50005;
GO

 

Guess you like

Origin www.cnblogs.com/lsgsanxiao/p/10947329.html