RAISERROR Percent % Sign usage

Have you ever wondered how to build a string with values for an error message?  One would think that RASIERROR could simply take a string that was concatenated together with variables. 

Something like RAISERROR ('My Error' + @myErrorString, 16,1)

Unfortunately, RAISERROR is not as intuitive as this. 

The syntax for RAISERROR is:

RAISERROR ({msg_id | msg_str}{, severity, state}
    [, argument
        
[,...n]] )
    [WITH option[,...n]]

Maybe you have seen the arguments passed into the error message string with percent sign.

RAISERROR ('My Error %s', 16,1, @myErrorString)

The % sign indicates the use of a parameter. 
d or i Signed integer
o Unsigned octal
p Pointer
s String
u Unsigned integer
x or X Unsigned hexadecimal

To pad some space a number must be placed infront of the type.

RAISERROR ('My Error %10s', 16,1, @myErrorString,@myErrorString2)

To justify simply use the + or - sign after the % sign.  To pad with a space, use `` (next to the 1 key)

If you pass more than one argument then you must use the percent signs in order. 

RAISERROR ('My Error %10s is not cool %10s', 16,1, @myErrorString,@myErrorString2)

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this entry.
Comments
Page: 1 of 1
Page: 1 of 1
Leave a comment

 Enter the above security code (required)

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.