2017-05-16 56 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

感謝您的回覆。它仍然給出像錯誤一樣的錯誤:整數的無效輸入語法:「|| quote_literal($ 5)||」 –

+0

在我提到更改之後,函數正在創建時沒有錯誤,所以這是另一個沒有被這個問題覆蓋的問題。你可能有更多的這些,但是對於這個你需要改變你的變量的聲明:'declare idle integer:='|| quote_literal($ 5)||';'這不是整數。這是字符串(varchar/text)。 Postgres給了你相同的解釋,通常客戶(如PgAdmin)指出錯誤的確切位置。 –