Oracle TREAT() Function
Oracle TREAT()
is a built-in function used to change the declared type of an expression.
Oracle TREAT()
Syntax
Here is the syntax for the Oracle TREAT()
function:
TREAT( expr AS [ REF ] [ schema. ]type | JSON )
Parameters
expr
-
Required. The expression being operated on.
[ REF ] [ schema. ]type | JSON
-
Required. The type to be declared as.
Return Value
The Oracle TREAT()
function returns.
Note that you must have the EXECUTE
object privilege on type
to use this function.
-
In
expr AS JSON
,expr
is a SQL data type containing JSON, such asCLOB
. -
In
expr AS type
,expr
andtype
must be user-defined object types, not including top-level collections. -
type
must be some supertype or subtype of the declared type ofexpr
. If the most specific type ofexpr
istype
(or a subtype oftype
), thenTREAT
returnsexpr
. If the most specific type ofexpr
is nottype
(or a subtype oftype
), thenTREAT
returnsNULL
. -
If the declared type of
expr
is aREF
type, onlyREF
can be specified. -
If the declared type of
expr
is aREF
to the source type ofexpr
, thentype
must be some subtype or supertype of the source type. If the most specific type ofDEREF
(expr
) istype
(or a subtype oftype
), thenTREAT
returnsexpr
. If the most specific type ofDEREF
(expr
) is nottype
(or a subtype oftype
), thenTREAT
returnsNULL
.
Oracle TREAT()
Example
The following statement retrieves the salary attribute for all people in the persons
table, with instances of people who are not employees having a value of null.
SELECT
name,
TREAT(VALUE(p) AS employee_t).salary salary
FROM persons p;
输出:
NAME SALARY
------------------------- ----------
Bob
Joe 100000
Tim 1000
Conclusion
Oracle TREAT()
is a built-in function used to change the declared type of an expression.