在postgres中使用LOOP的功能

时间:2017-03-21 16:01:22

标签: postgresql loops

我不知道当我尝试创建一个包含LOOPS的函数时,我发生了错误。有什么想法吗?

  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;

我得到的错误就是这个: 错误:语法错误在或附近&#34; LOOP&#34; 第60行:结束循环;

知道为什么吗?我想我离开的东西没有关闭,但我无法看到它。

1 个答案:

答案 0 :(得分:1)

将您的else if更正为elsifELSEIF。这应该有效。

详情为hier

  

关键词ELSIF也可以拼写为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;
........