2017-05-16 7 views
0

私はこの手順を書いている間、Postgresの新機能でエラーが発生しました。私はどこでミスをしていますか?postgresqlで関数を作成中にエラーが発生しました

[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; 
        ^

答えて

0

PostgreSQLにはELSE IFがありません。それらをELSIFに変更する必要があります。

+0

ご返信ありがとうございます。それでもエラーのようなエラーが発生しています:integerの入力構文が無効です: "|| quote_literal($ 5)||" –

+0

変更後も機能がエラーなしで作成されていますので、これは別の問題です。それ以上のものがあるかもしれませんが、このために変数の宣言を変更する必要があります: ''アイドル整数:= '|| quote_literal($ 5)||'; 'これは整数ではありません。これはstring(varchar/text)です。 Postgresはあなたに同じ説明を与えてくれました。通常、PgAdminのようなクライアントはエラーの正確な場所を指摘しています。 –

関連する問題