SQL 服务器异常消息格式

SQL Server Exception Message Format

在此页面 Sql Server Errors 1-999 上有一个 SQL 服务器错误列表和相关的消息文本。

例如:

Err 2627: Violation of %ls constraint '%.*ls'. Cannot insert duplicate key in object '%.*ls'.

此异常的示例是:

Violation of UNIQUE KEY constraint 'AK_AdvanceShipmentNotices_CustomerId_PurchaseOrderNumber'. Cannot insert duplicate key in object 'dbo.AdvanceShipmentNotices'. The duplicate key value is (4, 1000).

示例错误文本包含占位符:%ls 和 %.*ls。查看其余的错误示例,还有许多其他占位符,例如%d、%s、%S_DATE、%hs、%S_TS、%x 等等。

问题:这些占位符是在任何地方定义的,还是我只需要自己弄清楚它们的含义?

其他示例错误:

T-SQL RAISERROR 函数的文档提供了更多信息:

msg_str is a string of characters with optional embedded conversion specifications. Each conversion specification defines how a value in the argument list is formatted and placed into a field at the location of the conversion specification in msg_str. Conversion specifications have this format:

% [[flag] [width] [. precision] [{h | l}]] type

标志

Is a code that determines the spacing and justification of the substituted value.

代码:

  • -: 在给定的字段宽度内左对齐参数值。
  • +:如果值是有符号类型,则在参数值前面加上加号 (+) 或减号 (-)。
  • 0:在输出前加上零,直到达到最小宽度。当出现 0 和减号 (-) 时,忽略 0。
  • #:当与 o、x 或 X 格式一起使用时,井号 (#) 标志分别在任何非零值前面加上 0、0x 或 0X。当 d、i 或 u 以数字符号 (#) 标志开头时,该标志将被忽略。
  • </code>(空白):如果值是有符号且为正的,则在输出值前加上空格。当包含在加号 (+) 标志中时,它会被忽略。</li> </ul> <p><em>宽度</em></p> <blockquote> <p>Is an integer that defines the minimum width for the field into which the argument value is placed. If the length of the argument value is equal to or longer than width, the value is printed with no padding. If the value is shorter than width, the value is padded to the length specified in width. An asterisk (*) means that the width is specified by the associated argument in the argument list, which must be an integer value.</p> </blockquote> <p><em>精度</em></p> <blockquote> <p>Is the maximum number of characters taken from the argument value for string values. For example, if a string has five characters and precision is 3, only the first three characters of the string value are used. For integer values, precision is the minimum number of digits printed. An asterisk (*) means that the precision is specified by the associated argument in the argument list, which must be an integer value.</p> </blockquote> <p><em>{h | l}类型</em></p> <blockquote> <p>Is used with character types d, i, o, s, x, X, or u, and creates shortint (h) or longint (l) values.</p> </blockquote> <p>类型规格</p> <ul> <li><code>di:有符号整数
  • o: 无符号八进制
  • s: 字符串
  • u: 无符号整数
  • xX: 无符号十六进制

https://msdn.microsoft.com/en-us/library/ms178592.aspx