Dolphin
Dolphin

Reputation: 38691

sql raiserror specify parameter

When i read the example of MSDN raiserror:

RAISERROR (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

Why the doc using %s to specify the N'number',and %d to specify the 5 -- Second argument

The MSDN write like this:

For example, in the following RAISERROR statement, the first argument of N'number' replaces the first conversion specification of %s; and the second argument of 5 replaces the second conversion specification of %d.

My question is: How can explain it? Why don't using other like %a or %b.Any other %+apha can replace it. I just want to get a meaningful understand.

Upvotes: 6

Views: 8567

Answers (1)

Martin Smith
Martin Smith

Reputation: 453278

This represents the parameter datatype.

+--------------------+----------------------+
| Type specification |      Represents      |
+--------------------+----------------------+
| d or i             | Signed integer       |
| o                  | Unsigned octal       |
| s                  | String               |
| u                  | Unsigned integer     |
| x or X             | Unsigned hexadecimal |
+--------------------+----------------------+

N'number' is an nvarchar string literal. So gets the %sspecifier. And the literal 5 is a signed indicator so is represented by %d.

As for the reason for these specifiers. This is documented in the RAISERROR topic

These type specifications are based on the ones originally defined for the printf function in the C standard library

Upvotes: 11

Related Questions