【发布时间】:2021-11-30 14:31:53
【问题描述】:
将json数组的行插入表中的功能:
create table mytable(col1 text, col2 boolean, col3 boolean);
create function fun1(vja json[])
... as $$
begin
foreach v in array json_array_elements(vja)
loop
insert into mytable(col1, col2, col3)
values(v->'col1', v->'col2'::boolean, v->'col3'::boolean);
end loop;
end;
$$;
调用这个函数:
select fun1('[
{"col1": "turow1@af.com", "col2": false, "col3": true},
{"col1": "xy2@af.com", "col2": false, "col3": true}
]')
或这种形式:
select fun1('[
{"col1": "turow1@af.com", "col2": "false", "col3": "true"},
{"col1": "xy2@af.com", "col2": "false", "col3": "true"},
]')
或这种形式:
select fun1('[
{"col1": "turow1@af.com", "col2": "false", "col3": "true"},
{"col1": "xy2@af.com", "col2": "false", "col3": "true"},
]'::json[])
总是收到:
ERROR: malformed array literal: "[ {"col1": "turow1@af.com", "col2": "false", "col3": "true"}, {"col1": "xy2@af.com", "col2": "false", "col3": "true"}, ]" LINE 2: '[ ^ DETAIL: "[" must introduce explicitly-specified array dimensions. SQL state: 22P02 Character: 136
【问题讨论】:
标签: sql arrays json postgresql function