在创建 PostgreSQL 函数时,使用 Array_append 给了我语法错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9680065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-20 23:44:06  来源:igfitidea点击:

using Array_append gives me syntax error when creating PostgreSQL function

postgresqlarrays

提问by StanM

Here is the code

这是代码

CREATE OR REPLACE FUNCTION primes (IN   integer) RETURNS TEXT AS $$
DECLARE
    counter INTEGER = ;
    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'

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].

这是我开发素数生成函数的开端。我试图简单地让它返回数组的“计数”。因此,如果我将 '7' 传递给函数,我应该返回 [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(   [  ],   )
        ^ QUERY:  array_append(   [  ],   ) CONTEXT:  SQL statement in PL/PgSQL function "primes" near line 8

I am a newbie with postgres functions. I am not understanding why I cannnot get this array to work properly.

我是 postgres 函数的新手。我不明白为什么我不能让这个数组正常工作。

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).

同样,我想要的只是用数组的“当前”计数正确填充这个数组。(更多的是帮助我理解它实际上正在正确地执行循环并且正确地计算它)。

Thank you for your help.

谢谢您的帮助。

回答by mu is too short

From the fine manual:

精美的手册

Function: array_append(anyarray, anyelement)
Return Type: anyarray
Description: append an element to the end of an array

功能array_append(anyarray, anyelement)
返回类型anyarray
描述:在数组末尾追加一个元素

So array_appendreturns an array and you need to assign that return value to something. Also, I think you want array_to_stringat the end of your function, not array_to_text. And primesis an array so you want array_append(primes, mycount)rather than trying to append to an entry in primes.

所以array_append返回一个数组,你需要将该返回值分配给某个东西。另外,我认为你想要array_to_string在你的函数结束时,而不是array_to_text. Andprimes是一个数组,因此您想要array_append(primes, mycount)而不是尝试附加到primes.

CREATE OR REPLACE FUNCTION primes (IN integer) RETURNS TEXT AS $$
DECLARE
    counter INTEGER = ; 
    primes int []; 
    mycount int; 
BEGIN
    WHILE counter != 0 LOOP 
        mycount := count(primes); 
        primes  := array_append(primes, mycount);
        counter := counter - 1; 
    END LOOP;
    RETURN array_to_string(primes, ',');   
END;   
$$ LANGUAGE 'plpgsql';

I don't know what you intend mycount := count(primes);to do, perhaps you meant to say mycount := array_length(primes, 1);so that you would get a sequence of consecutive integers in primes.

我不知道你打算mycount := count(primes);做什么,也许你的意思是说mycount := array_length(primes, 1);你会在primes.