如何在循环内的postgres中初始化数组

时间:2014-02-26 15:49:56

标签: sql postgresql postgresql-8.4

我正在尝试初始化数组array_entries。尝试将array_fill作为array_fill(0,array_entries)但是dint工作。

create or replace function vin_temp_test1(k date,x varchar) RETURNS float AS $$
    declare
    array_entries int [];
    curs4  CURSOR FOR  select * from temp_table;
    record_type1 record;

    fetch curs4 into record_type1;
            exit when not found;
    loop
    -- trying to intialize the array array_entries here
        loop
     --filling the array inside this loop.
        end loop;
    end loop;

1 个答案:

答案 0 :(得分:0)

你可能在array_entries中有NULL

postgres=# select array_fill(0, NULL);
ERROR:  dimension array or low bound array cannot be null
postgres=# select array_fill(0, ARRAY[10]);
      array_fill       
-----------------------
{0,0,0,0,0,0,0,0,0,0}
(1 row)

<强>注意!

很高兴知道,因此大型阵列(大于20000个字段)的更新速度非常慢。比可重复更新快得多的是使用ARRAY(subselect)构造函数

postgres=# DO $$ DECLARE x int[]; 
           begin  
              x := array_fill(0,ARRAY[100000]); 
              for i in 1..100000 loop 
                x[i] := 1; 
           end loop; end $$;
DO
Time: 5533.581 ms
postgres=# DO $$ DECLARE x int[]; 
           begin  x := ARRAY(SELECT 1 FROM generate_series(1,100000)); end $$;
DO
Time: 36.590 ms