在此postgressql函数中,我通过拆分字符串创建了一个数组。现在我要在此数组上循环并对其进行一些处理。
功能:
CREATE OR REPLACE FUNCTION getAllFoo() RETURNS character varying as $BODY$ DECLARE arr_split_data text[]; counter character varying; begin counter := ''; -- Init value -- split data. Add in array select into arr_split_data regexp_split_to_array('a,b,c,d,e,f',','); FOR r IN arr_split_data -- error LOOP counter := arr_split_data[r] || '_' || counter; -- do some processing END LOOP; return counter; END $BODY$ LANGUAGE 'plpgsql';
但是我收到这个错误
当我执行此功能时。我的for循环语法错误吗?
语法是
FOREACH r IN ARRAY arr_split_data LOOP counter := r || '_' || counter; -- do some processing END LOOP;
您还需要声明r
:
DECLARE arr_split_data TEXT []; r CHARACTER VARYING; counter CHARACTER VARYING; BEGIN
请参见手册第41.6.5节:遍历数组