创建PostgreSQL函数时,使用Array_append给我语法错误 [英] using Array_append gives me syntax error when creating PostgreSQL function

查看:249
本文介绍了创建PostgreSQL函数时,使用Array_append给我语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是代码

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

谢谢您的帮助.

推荐答案

来自

功能:array_append(anyarray, anyelement)
返回类型:anyarray
说明:将元素附加到数组的末尾

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

因此,array_append返回一个数组,您需要将该返回值分配给某些对象.另外,我认为您想在功能末尾使用array_to_string,而不是array_to_text.而且primes是一个数组,因此您需要array_append(primes, mycount)而不是尝试附加到primes中的条目.

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

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); 
        primes  := array_append(primes, mycount);
        counter := counter - 1; 
    END LOOP;
    RETURN array_to_string(primes, ',');   
END;   
$$ LANGUAGE 'plpgsql';

我不知道您打算mycount := count(primes);做什么,也许您是想说mycount := array_length(primes, 1);以便您在primes中获得一系列连续的整数.

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.

这篇关于创建PostgreSQL函数时,使用Array_append给我语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆