Given this table,
CREATE TABLE test (
c01 INT,
c02 NUMBER(10,5),
c03 NUMBER(18,10)
);
I use OCCI (the C++ Library) to execute the following SELECT query:
select case(c01) when 10 then c02 else c03 end from test;
In this case, the typecode I get is OCCI_SQLT_NUM
but the precision and scale both are 0
. What does that (0
precision and scale) mean? Is this a bug?
Without knowing the precision/scale, the type-safety, semantics and the correctness of the program is at the stake, as it is not possible to decide whether to treat it as integer or floating-point.
In other words, what is the type of the CASE(x) WHEN y THEN a ELSE b
expression? Can a
be INT
and b
be CHAR[32]
? I think no. So how is the type computed?
In a
CASE
expression, the data types of the expressions in theTHEN
andELSE
clauses must be compatible. So you can't have aNUMBER
in theTHEN
clause and then aDATE
orVARCHAR2
in theELSE
clause.However, precision and scale are not part of the data type. It is better to think of precision and scale as constraints on the values that are allowed in a table.
NUMBER(10,2)
is the same data type asNUMBER
, but with the constraint that there should be no more than ten total digits, including the two reserved for the decimal part, and no more than two after the decimal point. (Then, inserting5.333
will still work - but only because Oracle will automatically and without warning round this number to fit into the column... but if you gave it the input123456123456
, there's no "rounding" that will make it fit so the insert will fail). The data type of yourCASE
expression is simplyNUMBER
. (Incidentally, in Oracle SQL, evenINT
is a constraint - Oracle DOES NOT DO integer arithmetic!!)NUMBER(0,0)
does not exist in Oracle; if you try toCREATE TABLE test (col NUMBER(0,0))
, it will fail. The error message will tell you that the precision (the first number) must be between 1 and 38. So whatever producedNUMBER(0,0)
sounds like a bug.