在postgresql中创建函数时出错

时间:2017-05-16 10:01:19

标签: postgresql temp-tables

我是postgres的新手并且在编写此程序时遇到错误,请帮助我解决它。我哪里弄错了?

[WARNING  ] CREATE OR REPLACE   FUNCTION idlereport_final (
            IN text,
            IN integer,
            IN text,
            IN text,
            IN integer,
            IN integer,
            IN text
            )
        RETURNS TABLE(sys_service_id bigint,stat integer,lat double precision, long double precision,
        beg_time timestamp without time zone,end_time timestamp without time zone, address character varying,duration interval) as 
        $BODY$

         declare idle integer:= '||quote_literal($5)||';
          declare stop integer:= '||quote_literal($6)||';
          begin
         EXECUTE 'create temp table temp1 as 
         SELECT  sys_service_Id, I2,tel_odometer, gps_time, gps_latitude, gps_longitude, gps_speed,address_from_device,
        case
        when (t.I2=1 and t.gps_speed<3)  then 5
         when (t.I2=1 and t.gps_speed>3) then  1
          else 2
           end as stat,
              row_number() OVER (ORDER BY  gps_time asc) as rn   FROM '||quote_ident($1)||'   where  sys_service_id='||quote_literal($2)||'  
              and gps_time between '||quote_literal($3)||' and '||quote_literal($4)||'';


        execute 'create temp table temp2 as
         select abc.*    ,ROW_NUMBER() OVER (ORDER BY  time1) as rn2  from (
            select  temp1.sys_service_id,  temp1.i2,  temp1.tel_odometer, temp1.gps_time as Time1, temp1.gps_latitude as lat1, temp1.gps_longitude 
            as long1,Y.gps_time as Time2,  temp1.gps_speed, 
            temp1.address_from_device as address1,y.stat from temp1
          LEFT OUTER JOIN  temp1 AS y
        ON  temp1.rn= y.rn+1   
        AND  temp1.stat <> y.stat 
        WHERE Y.i2 is not null
        union 
        select  sys_service_Id, I2,tel_odometer, gps_time, gps_latitude, gps_longitude,null ,gps_speed,address_from_device,stat   from temp1
        where rn=1
        union  
        select  sys_service_Id, I2,tel_odometer, gps_time, gps_latitude, gps_longitude,null ,gps_speed,address_from_device,stat  from temp1
        where rn=(select max(rn) from temp1) 
        ) abc';

        IF     idle=5 and stop =0
        THEN
        execute 'create temp table temp3 as  
        select   sys_service_id,  lat1 as lat, long ,  beg_time,  end_time, stat, new.address , trip_leg as duration    from
        (
        select    temp2.sys_service_id  ,temp2.stat stat,  x.Time1 beg_time, temp2.time1 as end_time, temp2.gps_speed, Temp2.I2,
         temp2.rn2,  temp2.lat1  , temp2.long1 long, temp2.address1 as address, temp2.Time1-x.Time1 
        as trip_leg  
         from  temp2 left outer join  temp2 as x
         on temp2.rn2=x.rn2+1
          )new
         where beg_time is not null 
         and new.trip_leg > '||quote_literal($7)||'
         and new.stat in (5)
         order by beg_time';


        else IF  idle=0 and stop =2
        THEN 
        execute 'create temp table temp3 as  
        select   sys_service_id,  lat1 as lat, long ,  beg_time,  end_time, stat, new.address , trip_leg as duration  from
        (
        select    temp2.sys_service_id  ,temp2.stat stat,  x.Time1 beg_time, temp2.time1 as end_time, temp2.gps_speed, Temp2.I2,
         temp2.rn2,  temp2.lat1  , temp2.long1 long, temp2.address1 as address, temp2.Time1-x.Time1 
        as trip_leg  
         from  temp2 left outer join  temp2 as x
         on temp2.rn2=x.rn2+1
          )new
         where beg_time is not null 
         and new.trip_leg >'||quote_literal($7)||'
         and new.stat in (2)
         order by beg_time';


        else  IF   idle=5 and stop=2
        THEN
        execute 'create temp table temp3 as  
        select   sys_service_id,  lat1 as lat, long ,  beg_time,  end_time, stat, new.address , trip_leg as duration   from
        (
        select    temp2.sys_service_id  ,temp2.stat stat,  x.Time1 beg_time, temp2.time1 as end_time, temp2.gps_speed, Temp2.I2,
         temp2.rn2,  temp2.lat1  , temp2.long1 long, temp2.address1 as address, temp2.Time1-x.Time1 as trip_leg  
         from  temp2 left outer join  temp2 as x
         on temp2.rn2=x.rn2+1
          )new
         where beg_time is not null 
         and new.trip_leg >'||quote_literal($7)||'
         and new.stat in (2,5)
         order by beg_time';
        end if;
         return query execute 'select * from temp3';
         drop table temp1;
         drop table  temp2;
         drop table temp3;
         end;
         $BODY$ LANGUAGE plpgsql
        ERROR:  syntax error at or near ";"
        LINE 106:  end;
                      ^

1 个答案:

答案 0 :(得分:0)

PostgreSQL中没有ELSE IF。您需要将这些更改为ELSIF