- 类别:
TO_VARIANT¶
将任何值转换为 VARIANT 值或 NULL(如果输入为 NULL)。
语法¶
TO_VARIANT( <expr> )
实参¶
expr
任何数据类型的表达式。
使用说明¶
TO_VARIANT
函数不能直接在 INSERT 语句中使用。请改用INSERT INTO ... SELECT...
。示例部分说明了如何执行此操作。
示例¶
使用 TO_VARIANT 将值插入到表中。
CREATE OR REPLACE TABLE to_variant_example (
v_varchar VARIANT,
v_number VARIANT,
v_timestamp VARIANT,
v_array VARIANT,
v_object VARIANT);
INSERT INTO to_variant_example (v_varchar, v_number, v_timestamp, v_array, v_object)
SELECT
TO_VARIANT('Skiing is fun!'),
TO_VARIANT(3.14),
TO_VARIANT('2024-01-25 01:02:03'),
TO_VARIANT(ARRAY_CONSTRUCT('San Mateo', 'Seattle', 'Berlin')),
TO_VARIANT(PARSE_JSON(' { "key1": "value1", "key2": "value2" } '));
SELECT * FROM to_variant_example;
+------------------+----------+-----------------------+----------------+---------------------+
| V_VARCHAR | V_NUMBER | V_TIMESTAMP | V_ARRAY | V_OBJECT |
|------------------+----------+-----------------------+----------------+---------------------|
| "Skiing is fun!" | 3.14 | "2024-01-25 01:02:03" | [ | { |
| | | | "San Mateo", | "key1": "value1", |
| | | | "Seattle", | "key2": "value2" |
| | | | "Berlin" | } |
| | | | ] | |
+------------------+----------+-----------------------+----------------+---------------------+