Concatenating numbers in virtual column expression throws ORA-12899: value too large for column

后端 未结 1 1418
忘掉有多难
忘掉有多难 2021-01-05 04:20

While I gave this answer to a question yesterday, I suggested to use a VIRTUAL COLUMN for computed values instead of manually updating it.

I did a t

相关标签:
1条回答
  • 2021-01-05 04:49

    Your numbers are not constrained. With single digit (positive) numbers you know the concatendated length can only be three, but the virtual column has to be large enough for any number - so it looks like it's allowing up to 40 digits for the implicit format model (38 significant digits, the decimal separator, and the sign; @collspar's lexicalisation).

    Having said that, constraining the number column wouldn't be reflected in the virtual column length - making both columns NUMBER(1,0) still leaves the concatenation requiring 81 characters. Taking the substring of the generated value won't work either, in this case getting ORA-12899: value too large for column "TEXT" (actual: 10, maximum: 40). Supplying a format model for each to_char() call, e.g. of FM999), would work but restricts the values either side of the underscore rather than the overall length directly.

    If you want to restrict the column size, you can cast it to the same data type and size, which is more explicit:

    text VARCHAR2(10) generated always as 
        (cast(to_char(id)||'_'||to_char(num) as VARCHAR2(10))) VIRTUAL
    
    0 讨论(0)
提交回复
热议问题