我必须插入值[{name:'ravi',age;20},{name:'kumar',age;22}]
。存储过程应该将此数组作为输入,并将其存储为单独的列,如下所示。
表
name - age
ravi 20
age 22
插入json对象的过程:
CREATE OR REPLACE FUNCTION insertJson(jsonObjects ARRAY[])
RETURNS void AS $$
BEGIN
INSERT INTO MyTable
VALUES (jsonObjects)
END;
$$ LANGUAGE plpgsql;
答案 0 :(得分:0)
CREATE OR REPLACE FUNCTION insertJson(obj JSONB)
RETURNS void AS $$
BEGIN
INSERT INTO test2 (name, "age")
SELECT arr.row ->> 'name', (arr.row ->> 'age')::numeric FROM (SELECT jsonb_array_elements(obj) as row) as arr;
END;
$$ LANGUAGE plpgsql;