Integer 或 Smallint 用于数字文字

Integer or Smallint for numeric literal

下面的查询结果让我很生气:

select
  char(coalesce(1, cast(5 as decimal(2,0)))) as a,
  char(1 * cast(5 as decimal(2, 0))) as b
from sysibm.sysdummy1;

a = ' 00000000001.'
b = ' 0000005.'

第一个选择的 returned 值显然是 decimal(11,0) 类型。 coalesce 函数的类型统一记录在 here(请参阅数字操作数)。按照这些说明并向后解析 decimal(11,0) 意味着文字 1 被解释为一个大整数 .

第二个选择的return类型是小数(7,0)。可以读取 DB2 在乘法上的行为方式 here(结果的精度是两个操作数的精度之和)。在这种情况下,这意味着文字 1 被解释为一个小整数 .

请注意,char(..) 函数仅用于显示 returned 值的精度和小数位数。

当有整数文字时(如上例中的 1),我如何知道 DB2 如何解释它(smallint、largeint、bigint)?

我正在为 z/OS v11 使用 DB2。

所有适合大整数的数字常量都被这样解释 - 除非它们不是,谢谢 IBM ;)。

在第二种情况下,您必须查看文字出现的上下文,即在算术表达式中。 SQLReference 的 "Arithmetic with an integer and a decimal operand" 部分明确描述了该行为:

The temporary copy of the integer that has been converted to a decimal number has a precision p and scale 0. p is 19 for a big integer, 11 for a large integer, and 5 for a small integer. In the case of an integer constant, p depends on the number of digits in the integer constant. p is 5 for an integer constant consisting of 5 digits or fewer. Otherwise, p is the same as the number of digits in the integer constant.

因此,对常量的处理不同于各种 "builtin" 二进制数据类型。常量值(尽管定义为二进制)更像是十进制常量。