Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 412
Dolphin Avatar asked Oct 20 '13 12:10

Dolphin


People also ask

What is state in Raiserror SQL?

Is an integer from 0 through 255. Negative values default to 1. Values larger than 255 should not be used. If the same user-defined error is raised at multiple locations, using a unique state number for each location can help find which section of code is raising the errors.

How do I create a custom error message in SQL Server?

Therefore, when a system error occurs, SQL Server will log a system error and may take actions to fix the error. Custom errors, on the other hand, are generated by T-SQL custom codes based on your code or business logic. To add a custom error message to sys. messages, the stored procedure sp_addmessage is used.

What is the difference between Raiserror and throw?

Differences Between RAISERROR and THROWIf a msg_id is passed to RAISERROR, the ID must be defined in sys. messages. The error_number parameter does not have to be defined in sys. messages.


1 Answers

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

like image 84
Martin Smith Avatar answered Sep 23 '22 01:09

Martin Smith