创建PostgreSQL函数时,使用Array_append给我语法错误 [英] using Array_append gives me syntax error when creating PostgreSQL function
问题描述
这是代码
CREATE OR REPLACE FUNCTION primes (IN integer) RETURNS TEXT AS $$
DECLARE
counter INTEGER = $1;
primes int [];
mycount int;
BEGIN
WHILE counter != 0 LOOP
mycount := count(primes);
array_append(primes [counter], mycount);
counter := counter - 1;
END LOOP;
RETURN array_to_text(primes[], ',');
END;
$$
LANGUAGE 'plpgsql'
这是我开发素数生成函数的起点.我试图简单地使其返回数组的计数".因此,如果我将'7'传递给函数,我应该返回[0,1,2,3,4,5,6].
This is me developing the beginnings of a prime generating function. I am trying to simply get it to return the 'count' of the array. So if I pass '7' into the function I should get back [0, 1, 2, 3, 4, 5, 6].
但是当我尝试创建此功能时,我会得到
But when I try to create this function I get
SQL Error: ERROR: syntax error at or near "array_append" LINE 1: array_append( $1 [ $2 ], $3 )
^ QUERY: array_append( $1 [ $2 ], $3 ) CONTEXT: SQL statement in PL/PgSQL function "primes" near line 8
我是具有Postgres功能的新手.我不明白为什么我无法使该数组正常工作.
I am a newbie with postgres functions. I am not understanding why I cannnot get this array to work properly.
我只想用数组的当前"计数正确地填充该数组. (更多是为了帮助我了解它实际上是在正确执行循环并正确计数).
Again all I want is to just fill this array up correctly with the 'current' count of the array. (It's more to just help me understand that it is in fact doing the loop correctly and is counting it correctly).
谢谢您的帮助.