2017-03-21 2 views
0

ich ratlos bin ich mit einem Fehler kommen, wenn ich versuche, eine Funktion mit einigen LOOPS darin zu schaffen. Irgendeine Idee, was ist das Problem damit?Funktion mit LOOP in Postgres

create or replace function prueba() 
RETURNS varchar as $$ 
    declare 
     hores varchar[] :=ARRAY['0:00-1:00', 
         '1:00-2:00', 
         '2:00-3:00', 
         '3:00-4:00', 
         '4:00-5:00', 
         '5:00-6:00', 
         '6:00-7:00', 
         '7:00-8:00', 
         '8:00-9:00', 
         '9:00-10:00', 
         '10:00-11:00', 
         '11:00-12:00', 
         '12:00-13:00', 
         '13:00-14:00', 
         '14:00-15:00', 
         '15:00-16:00', 
         '16:00-17:00', 
         '17:00-18:00', 
         '18:00-19:00', 
         '19:00-20:00', 
         '20:00-21:00', 
         '21:00-22:00', 
         '22:00-23:00', 
         '23:00-24:00'];  
     dies date; 
     cur_acd CURSOR FOR SELECT * FROM acd; 
     row_acd acd%ROWTYPE; 
     ateses int:=0; 
     ateses_10 int :=0; 
     ateses_30 int:=0; 
     ateses_60 int:=0; 
     ateses_more int:=0; 
    begin 


     for dies in select distinct acd.dia::date from acd order by 1 LOOP 
      FOR i in 0..23 LOOP 

       OPEN cur_acd; 
       FETCH cur_acd INTO row_acd; 
       EXIT WHEN NOT FOUND;  

       if row_acd.dia::date = dies and to_char(row_acd.dia,'HH24')=i and durada < 10 then 
        ateses_10=ateses_10+1; 
       else if row_acd.dia::date = dies and to_char(row_acd.dia,'HH24')=i and durada < 30 and durada >= 10 then 
        ateses_30=ateses_30+1; 
       else if row_acd.dia::date = dies and to_char(row_acd.dia,'HH24')=i and durada < 60 and durada >= 30 then 
        ateses_60=ateses_60+1; 
       else if row_acd.dia::date = dies and to_char(row_acd.dia,'HH24')=i and durada > 60 then 
        ateses_more=ateses_more+1; 
       else 
        ateses=0; 
       end if; 
       INSERT INTO acd_detall values (dies,hores[i+1],ateses_10,ateses_30,ateses_60,ateses_more,ateses); 
      END LOOP; 
     END LOOP; 
     close cur_acd; 
     return 'Todo ha ido bien'; 
    end; 
$$ LANGUAGE plpgsql; 

Der Fehler, der i erhalten, ist dieses: ERROR: Syntaxfehler bei oder nahe bei "loop" LINE 60: END LOOP;

Irgendeine Idee warum? Ich denke, ich habe etwas ohne zu schließen, aber ich kann es nicht sehen.

Antwort

1

Korrigieren Sie Ihre else if zu elsif oder ELSEIF. das sollte funktionieren.

Einzelheiten sind hier

The key word ELSIF can also be spelled ELSEIF.

....... 
elsif row_acd.dia::date = dies and to_char(row_acd.dia,'HH24')=i and durada < 30 and durada >= 10 then 
       ateses_30=ateses_30+1; 
elsif row_acd.dia::date = dies and to_char(row_acd.dia,'HH24')=i and durada < 60 and durada >= 30 then 
       ateses_60=ateses_60+1; 
elsif row_acd.dia::date = dies and to_char(row_acd.dia,'HH24')=i and durada > 60 then 
ateses_more=ateses_more+1; 
........ 
+0

Es war das! Ich habe Java gelernt und ich habe sie gemischt, danke! –