类别:

半结构化和结构化数据函数 (类型转换)

AS_OBJECT

将 VARIANT 值转换为对象。

另请参阅:

AS_<object_type>AS_ARRAY

语法

AS_OBJECT( <variant_expr> )
Copy

实参

variant_expr

VARIANT 类型值计算的表达式。

使用说明

示例

这显示了该函数的使用方法:

创建表和数据:

CREATE TABLE multiple_types (
    array1 VARIANT,
    array2 VARIANT,
    boolean1 VARIANT,
    char1 VARIANT,
    varchar1 VARIANT,
    decimal1 VARIANT,
    double1 VARIANT,
    integer1 VARIANT,
    object1 VARIANT
    );
INSERT INTO multiple_types 
     (array1, array2, boolean1, char1, varchar1, 
      decimal1, double1, integer1, object1)
   SELECT 
     TO_VARIANT(TO_ARRAY('Example')), 
     TO_VARIANT(ARRAY_CONSTRUCT('Array-like', 'example')), 
     TO_VARIANT(TRUE), 
     TO_VARIANT('X'), 
     TO_VARIANT('I am a real character'), 
     TO_VARIANT(1.23::DECIMAL(6, 3)),
     TO_VARIANT(3.21::DOUBLE),
     TO_VARIANT(15),
     TO_VARIANT(TO_OBJECT(PARSE_JSON('{"Tree": "Pine"}')))
     ;
Copy

现在运行查询:

SELECT 
       AS_ARRAY(array1) AS "ARRAY1",
       AS_ARRAY(array2) AS "ARRAY2",
       AS_BOOLEAN(boolean1) AS "BOOLEAN",
       AS_CHAR(char1) AS "CHAR",
       AS_DECIMAL(decimal1, 6, 3) AS "DECIMAL",
       AS_DOUBLE(double1) AS "DOUBLE",
       AS_INTEGER(integer1) AS "INTEGER",
       AS_OBJECT(object1) AS "OBJECT",
       AS_ARRAY(object1) AS "OBJECT AS ARRAY"
  FROM multiple_types;
+-------------+-----------------+---------+------+---------+--------+---------+------------------+-----------------+
| ARRAY1      | ARRAY2          | BOOLEAN | CHAR | DECIMAL | DOUBLE | INTEGER | OBJECT           | OBJECT AS ARRAY |
|-------------+-----------------+---------+------+---------+--------+---------+------------------+-----------------|
| [           | [               | True    | X    |   1.230 |   3.21 |      15 | {                | NULL            |
|   "Example" |   "Array-like", |         |      |         |        |         |   "Tree": "Pine" |                 |
| ]           |   "example"     |         |      |         |        |         | }                |                 |
|             | ]               |         |      |         |        |         |                  |                 |
+-------------+-----------------+---------+------+---------+--------+---------+------------------+-----------------+
Copy
语言: 中文