2014-02-26 4 views
1

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; 

답변

0

아마 당신은 array_entries

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) 

주의에 NULL이!

알아두면 좋겠지 만, 큰 배열 (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 
+0

실제로 선언 섹션 다음에 배열을 원했습니다. 그 안에 값이없는 빈 배열을 의미합니다. – user2569524

관련 문제