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,expris a SQL data type containing JSON, such asCLOB. -
In
expr AS type,exprandtypemust be user-defined object types, not including top-level collections. -
typemust be some supertype or subtype of the declared type ofexpr. If the most specific type ofexpristype(or a subtype oftype), thenTREATreturnsexpr. If the most specific type ofexpris nottype(or a subtype oftype), thenTREATreturnsNULL. -
If the declared type of
expris aREFtype, onlyREFcan be specified. -
If the declared type of
expris aREFto the source type ofexpr, thentypemust be some subtype or supertype of the source type. If the most specific type ofDEREF(expr) istype(or a subtype oftype), thenTREATreturnsexpr. If the most specific type ofDEREF(expr) is nottype(or a subtype oftype), thenTREATreturnsNULL.
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 1000Conclusion
Oracle TREAT() is a built-in function used to change the declared type of an expression.