[英]Iterating on each element from an array of arrays on Postgresql
I have a VARCHAR of numbers inside my stored procedure, these numbers are organized as arrays, I will show an example below: 我的存储过程中有一个数字的VARCHAR,这些数字被组织为数组,下面将显示一个示例:
{1,2,3,4,5,6,7,8,9},{1,2,3,4,5},{1,2,3},{9} -- This is a STRING
Now with a help from another guy from here I'm using this to get integer arrays integer[]
现在,在这里另一个人的帮助下,我正在使用它来获取整数数组
integer[]
SELECT string_to_array(regexp_split_to_table(
trim('{1,2,3,4,5,6,7,8,9},{1,2,3,4,5},{1,2,3},{9}', '{}')
, '},{'), ',')::int[]
I will have a set of records, each of them with an array, see below: 我将有一组记录,每个记录都有一个数组,请参见下文:
{1,2,3,4,5,6,7,8,9}
{1,2,3,4,5}
{1,2,3}
{9}
I was trying but I cannot figure out how can I make a FOR
to iterate over each element from these arrays to call another procecure to do with each element from each array. 我正在尝试,但是我无法弄清楚如何使
FOR
可以遍历这些数组中的每个元素,以调用另一个过程来处理每个数组中的每个元素。
An example for my array {1,2,3,4,5,6,7,8,9}
that I will call my_array
: 我的数组
{1,2,3,4,5,6,7,8,9}
的示例将称为my_array
:
rec record;
arr integer[];
FOR rec IN SELECT string_to_array(unnest(string_to_array(trim(text_nodes_for_connectivity, '{}'), '},{')), ',')::int[] LOOP
arr := array_cat(arr, rec);
END LOOP;
I'm getting this error: 我收到此错误:
function array_cat(integer[], record) does not exist
I need to convert each of my record results to an array, so I can use array_cat or another functions to iterate over array elements 我需要将每个记录结果转换为一个数组,因此我可以使用array_cat或其他函数来遍历数组元素
My proc code goes below: 我的proc代码如下:
DROP FUNCTION IF EXISTS clustering_nodes();
CREATE OR REPLACE FUNCTION clustering_nodes() RETURNS integer[] AS $$
DECLARE
my_array integer[];
rec record;
arr integer[];
my_var varchar[500];
len integer;
BEGIN
my_var = '{1,2,3,4,5,6,7,8,9},{1,2,3,4,5},{1,2,3},{8}';
FOR rec IN SELECT string_to_array(unnest(string_to_array(trim(my_var, '{}'), '},{')), ',')::int[] LOOP
len = array_length(rec);
arr := array_append(arr, len);
END LOOP;
RETURN arr;
END;
$$ LANGUAGE 'plpgsql' STRICT;
select clustering_nodes();
Tips or triks? 提示或技巧?
CREATE OR REPLACE FUNCTION clustering_nodes()
RETURNS integer[] AS
$func$
DECLARE
my_var text := '{1,2,3,4,5,6,7,8,9},{1,2,3,4,5},{1,2,3},{8}';
my_array integer[];
arr_len integer[];
BEGIN
FOR my_array IN
SELECT string_to_array(regexp_split_to_table(
trim(my_var, '{}'), '},{'), ',')::int[]
LOOP
arr_len := array_append(arr_len, array_upper(my_array, 1));
END LOOP;
RETURN arr_len;
END
$func$ LANGUAGE plpgsql;
array_length()
doesn't work on records, only on array. array_length()
对记录无效,仅对数组有效。
What's more, there is no array_length()
in Postgres 8.3 . 而且, Postgres 8.3中没有
array_length()
。 The manual is instrumental in figuring that out yourself. 该手册有助于弄清您自己。 Using the less favorable
array_upper()
instead. array_upper()
不太受欢迎的array_upper()
。
Assignment operator in plpgsql is :=
. plpgsql中的赋值运算符是
:=
。 Use of =
is undocumented. =
使用未记录。
plpgsql
is an identifier in LANGUAGE plpgsql
, not a string. plpgsql
是LANGUAGE plpgsql
的标识符,而不是字符串。 Do not quote it. 不要引用它。 May lead to sneaky errors.
可能会导致偷偷摸摸的错误。
You can assign variables at declaration time. 您可以在声明时分配变量。
STRICT
modifier is pointless without parameters. 没有参数的
STRICT
修饰符是毫无意义的。
-> SQLfiddle demo for Postgres 8.3. ->适用于Postgres 8.3的SQLfiddle演示 。
Again, this could be had in a single (if somewhat complex) call: 同样,这可以在单个(如果有些复杂)调用中进行:
SELECT array_agg(array_length(string_to_array(txt, ','), 1))
FROM unnest(string_to_array(
trim('{1,2,3,4,5,6,7,8,9},{1,2,3,4,5},{1,2,3},{8}', '{}')
, '},{')
) AS sub(txt);
You need to upgrade to a current version. 您需要升级到当前版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.