【发布时间】:2017-01-24 19:30:08
【问题描述】:
如何循环访问 Postgres 9.6 中 PLPGSQL 函数内的 jsonb 字段中包含的数组?
这是我的字段声明:
CREATE TABLE afact_rule(
...
expressions jsonb,
...
)
这是我的功能:
FOR expression IN SELECT * FROM json_array_elements(rule.expressions) LOOP
CASE expression.field
WHEN 'task_id' THEN
... whatever ...
END CASE;
END LOOP;
这是我使用它时遇到的错误:
LINE 1: SELECT * FROM json_array_elements(rule.expressions) ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts. QUERY: SELECT * FROM json_array_elements(rule.expressions)
循环遍历jsonb 字段中包含的数组的最佳(和有效)方法是什么?
rule.expressions的内容是:
[{"field": "dep_id", "value": 1, "operator_code": 1},
{"field": "title", "value": "customer", "operator_code": 2}]
【问题讨论】:
-
select * from "rule" r, json_array_elements(r.expressions) -
@a_horse_with_no_name,不,也不起作用。我认为该错误意味着 Postgres 中没有接受
jsonb参数的函数。所以,jsonb肯定还有其他功能 -
啊,没看到
jsonB。为此,您当然必须使用jsonB_array_length -
@a_horse_with_no_name,但 array_length 只会返回一个整数,我需要一个数组来循环它通过
FOR块。jsonB的所有函数都有B结尾吗?看起来像未记录的功能。 -
@a_horse_with_no_name ,抱歉,它已记录在案,我正在查看首先出现在 Google 中的 9.1 文档
标签: arrays json postgresql plpgsql