douyalin2258 2019-02-25 01:27
浏览 528
已采纳

如何在postgresql 9.6中将json数组作为函数参数传递

--Takes a json array to parse each elements as text .But it shows an error. --Change the type from json[] to json but not helps.

CREATE OR REPLACE FUNCTION public.test(in_param json[])
RETURNS integer
LANGUAGE 'plpgsql'
COST 100
VOLATILE 
AS $BODY$
declare 
 item text;
 arr1 text[];
 begin 
   for item in select * from json_array_elements_text(in_param ) loop
   arr1 = arr1||item;
   end loop;
return 0;
end;
$BODY$;
ALTER FUNCTION public.test(json[])
OWNER TO postgres;
  • 写回答

2条回答 默认 最新

  • dongnigeng1295 2019-02-25 01:52
    关注

    I think you're mistaken. ["a","b","c"] cannot be cast to json[]. Appending square brackets ([]) creates a Postgres array datatype and is not self castable to a JSON array type

    You'll receive this if you did so.

    ERROR: malformed array literal

    Rather, define your function to accept a simple JSON type

    create or replace function test(json_in json) ..
    ..
    

    Now, simply quoting your sample argument ["a","b","c"] will work

    select  test('["a","b","c"]')
    

    Demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部