【问题标题】:converting SETOF into a string将 SETOF 转换为字符串
【发布时间】:2017-02-28 09:35:05
【问题描述】:

函数json_object_keys(json) 返回一个setof text。如何将此 setof text 转换为所有元素由 ',' 分隔的字符串?我必须使用函数array_to_string(),但它接受一个数组,而不是setof。那么如何将setof 转换为数组。例如:

DECLARE 
    custom_fields   json;
BEGIN
    custom_fields:='{"name":"John Smith","age":23}';
    keys:=array_to_string(json_object_keys(custom_fields),','::text);
END

以上不起作用,我得到:

ERROR:  function array_to_string(text, text) does not exist

那么,如何将SETOF 转换为ARRAY

【问题讨论】:

    标签: postgresql plpgsql


    【解决方案1】:

    函数json_object_keys() 是一个集合返回函数,因此您应该将它用作行源(= 在FROM 子句中)。使用string_agg() 函数,您将获得结果:

    SELECT string_agg(cf, ',') INTO keys
    FROM json_object_keys(custom_fields) jok(cf);
    

    请注意,由于 INTO 子句,这仅适用于 PL/pgSQL 函数。

    【讨论】:

      猜你喜欢
      • 2019-10-10
      • 2019-01-05
      • 2015-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-29
      相关资源
      最近更新 更多