postgresql - Syntax error at or near "unnest" -
this request:
unnest('{1,2}'::int[]);
gives me error:
syntax error @ or near "unnest"
neither unnest('{1,2}');
works
why?
intire:
create or replace function result() returns setof users $$ declare begin unnest('{1,2}'::int[]); return query select * users; end; $$ language plpgsql; select result();
edit core idea:
to retrive , manipualate bigint[] stored inside in column. so, have got this:
select * users email = email_ limit 1 usr;
then, usr.chain contains bigint[]
data. example, {1,2,3,4,5,6,7,8,9,10}
. want save 4 last of them.
how retrieve {7,8,9,10}
, {1,2,3,4,5,6}
, iterate on these arrays?
i found solution use select unnest(usr.chain) x order x asc limit (sdl - mdl) offset mchain
, on. unnest
function gives me stupid error. i'm not understand why happends. doesn't work in sucj easy case wrote @ beginning of question. subarray
function doesn't work because of data type bigint[]
not int[]
futher more, code unnest(array[1,2])
gives me same error. http://www.postgresql.org/docs/9.2/static/functions-array.html same error array_append
function
to iterate on array:
create or replace function someresult(somearr bigint[] ) returns setof bigint $$ declare integer; x bigint; begin x in select unnest($1) loop -- return next x; end loop; -- or in array_lower($1, 1) .. array_upper($1, 1) loop -- like: return next ($1)[i]; end loop; end; $$ language plpgsql; select someresult('{1,2,3,4}') ;
array_append ....
create or replace function someresult2(somearr bigint[],val bigint ) returns bigint[] $$ declare somenew_arr bigint[]; begin somenew_arr = array_append($1, $2 ); return somenew_arr; end; $$ language plpgsql; select someresult2('{1,2,3,4}' ,222) ;
so, here have basic example how iterate , append arrays. can write step step want do, achieve .
Comments
Post a Comment