使用 json 更新列值
update column value with a json
如果我尝试使用数字列:
UPDATE TEST_TABLE SET META_ROW_NUM = 2
有效。但是,当我尝试使用 JSON 更新另一个字段时(也尝试不带引号“”):
UPDATE TEST_TABLE
SET JSON_DATA = "{
"Business_Type": "载货",
"Collected_Article_Quantity": null,
"Consignee_Company_ContactPerson": null,
"Consignee_Company_Email": null,
}"
我遇到这样的语法错误:
SQL Error [1003] [42000]: SQL compilation error:
syntax error line 3 at position 1 unexpected 'Business_Type'.
TYPEOF(JSON_DATA)
是对象。当我将鼠标悬停在 Dbeaver 中的 col 上时,我看到类型是 Variant:
table 看起来像这样:
使用 '
和 PARSE_JSON/TRY_PARSE_JSON:
UPDATE TEST_TABLE
SET JSON_DATA = PARSE_JSON('{
"Business_Type": "载货",
"Collected_Article_Quantity": null,
"Consignee_Company_ContactPerson": null,
"Consignee_Company_Email": null
}');
完整演示:
CREATE OR REPLACE TABLE t(JSON_DATA VARIANT);
INSERT INTO t VALUES(NULL);
UPDATE t
SET JSON_DATA = PARSE_JSON('{
"Business_Type": "载货",
"Collected_Article_Quantity": null,
"Consignee_Company_ContactPerson": null,
"Consignee_Company_Email": null
}');
SELECT * FROM t;
输出:
看来您可能想使用 OBJECT_CONSTRUCT 来构建您的值,它有一个很好的副作用,即不会插入具有 NULL
值的属性
因此:
SELECT column1 as id,
OBJECT_construct(column2, column3, column4, column5, column6, column7, column8, column9) as json
FROM VALUES
(1, 'Type', 'xxi', 'Quantity', null, 'ContactPerson', null, 'Email', null),
(2, 'Type', 'xxi', 'Quantity', 'many', 'ContactPerson', null, 'Email', null),
(3, 'Type', 'xxi', 'Quantity', 'many', 'ContactPerson', 'Simeon', 'Email', null),
(4, 'Type', 'xxi', 'Quantity', 'many', 'ContactPerson', 'Simeon', 'Email', 'example@example.com')
;
给出:
ID
JSON
1
{ "Type": "xxi" }
2
{ "Quantity": "many", "Type": "xxi" }
3
{ "ContactPerson": "Simeon", "Quantity": "many", "Type": "xxi" }
4
{ "ContactPerson": "Simeon", "Email": "example@example.com", "Quantity": "many", "Type": "xxi" }
将 JSON 设置为用于决定更新内容的相关数据的值“更”有意义。
但是如果你的数据是通过构建的,那么 OBJECT_AGG 工作得非常好:
,OBJECT_AGG(column2, column3) as json
FROM VALUES
(1, 'Type', 'xxi'),
(1, 'Quantity', null),
(1, 'ContactPerson', null),
(1, 'Email', null),
(2, 'Type', 'xxi'),
(2, 'Quantity', 'many'),
(2, 'ContactPerson', null),
(2, 'Email', null),
(3, 'Type', 'xxi', l),
(3, 'Quantity', 'many'),
(3, 'ContactPerson', 'Simeon'),
(3, 'Email', null),
(4, 'Type', 'xxi'),
(4, 'Quantity', 'many'),
(4, 'ContactPerson', 'Simeon'),
(4, 'Email', 'example@example.com')
GROUP BY 1
;
给出:
ID
JSON
1
{ "Type": "xxi" }
2
{ "Quantity": "many", "Type": "xxi" }
3
{ "ContactPerson": "Simeon", "Quantity": "many", "Type": "xxi" }
4
{ "ContactPerson": "Simeon", "Email": "example@example.com", "Quantity": "many", "Type": "xxi" }
如果我尝试使用数字列:
UPDATE TEST_TABLE SET META_ROW_NUM = 2
有效。但是,当我尝试使用 JSON 更新另一个字段时(也尝试不带引号“”):
UPDATE TEST_TABLE
SET JSON_DATA = "{
"Business_Type": "载货",
"Collected_Article_Quantity": null,
"Consignee_Company_ContactPerson": null,
"Consignee_Company_Email": null,
}"
我遇到这样的语法错误:
SQL Error [1003] [42000]: SQL compilation error:
syntax error line 3 at position 1 unexpected 'Business_Type'.
TYPEOF(JSON_DATA)
是对象。当我将鼠标悬停在 Dbeaver 中的 col 上时,我看到类型是 Variant:
table 看起来像这样:
使用 '
和 PARSE_JSON/TRY_PARSE_JSON:
UPDATE TEST_TABLE
SET JSON_DATA = PARSE_JSON('{
"Business_Type": "载货",
"Collected_Article_Quantity": null,
"Consignee_Company_ContactPerson": null,
"Consignee_Company_Email": null
}');
完整演示:
CREATE OR REPLACE TABLE t(JSON_DATA VARIANT);
INSERT INTO t VALUES(NULL);
UPDATE t
SET JSON_DATA = PARSE_JSON('{
"Business_Type": "载货",
"Collected_Article_Quantity": null,
"Consignee_Company_ContactPerson": null,
"Consignee_Company_Email": null
}');
SELECT * FROM t;
输出:
看来您可能想使用 OBJECT_CONSTRUCT 来构建您的值,它有一个很好的副作用,即不会插入具有 NULL
值的属性
因此:
SELECT column1 as id,
OBJECT_construct(column2, column3, column4, column5, column6, column7, column8, column9) as json
FROM VALUES
(1, 'Type', 'xxi', 'Quantity', null, 'ContactPerson', null, 'Email', null),
(2, 'Type', 'xxi', 'Quantity', 'many', 'ContactPerson', null, 'Email', null),
(3, 'Type', 'xxi', 'Quantity', 'many', 'ContactPerson', 'Simeon', 'Email', null),
(4, 'Type', 'xxi', 'Quantity', 'many', 'ContactPerson', 'Simeon', 'Email', 'example@example.com')
;
给出:
ID | JSON |
---|---|
1 | { "Type": "xxi" } |
2 | { "Quantity": "many", "Type": "xxi" } |
3 | { "ContactPerson": "Simeon", "Quantity": "many", "Type": "xxi" } |
4 | { "ContactPerson": "Simeon", "Email": "example@example.com", "Quantity": "many", "Type": "xxi" } |
将 JSON 设置为用于决定更新内容的相关数据的值“更”有意义。
但是如果你的数据是通过构建的,那么 OBJECT_AGG 工作得非常好:
,OBJECT_AGG(column2, column3) as json
FROM VALUES
(1, 'Type', 'xxi'),
(1, 'Quantity', null),
(1, 'ContactPerson', null),
(1, 'Email', null),
(2, 'Type', 'xxi'),
(2, 'Quantity', 'many'),
(2, 'ContactPerson', null),
(2, 'Email', null),
(3, 'Type', 'xxi', l),
(3, 'Quantity', 'many'),
(3, 'ContactPerson', 'Simeon'),
(3, 'Email', null),
(4, 'Type', 'xxi'),
(4, 'Quantity', 'many'),
(4, 'ContactPerson', 'Simeon'),
(4, 'Email', 'example@example.com')
GROUP BY 1
;
给出:
ID | JSON |
---|---|
1 | { "Type": "xxi" } |
2 | { "Quantity": "many", "Type": "xxi" } |
3 | { "ContactPerson": "Simeon", "Quantity": "many", "Type": "xxi" } |
4 | { "ContactPerson": "Simeon", "Email": "example@example.com", "Quantity": "many", "Type": "xxi" } |