2017-05-16 8 views
0

Ich bin neu zu Postgres und bekommen Fehler beim Schreiben dieses Verfahrens bitte helfen Sie mir, es zu lösen. Wo mache ich einen Fehler?Fehler beim Erstellen von Funktion in 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; 
        ^

Antwort

0

In PostgreSQL gibt es keine ELSE IF. Sie müssen diese in ELSIF ändern.

+0

danke für Ihre Antwort. Trotzdem gibt es einen Fehler wie ERROR: Ungültige Eingabesyntax für Integer: "|| quote_literal ($ 5) ||" –

+0

Funktion wird ohne Fehler nach Änderung erstellt, die ich erwähnt habe, also ist dies ein anderes Problem, das nicht von dieser Frage abgedeckt wird. Sie könnten mehr davon haben, aber für diesen müssen Sie die Deklaration für Ihre Variablen ändern: 'declare idle integer: = '|| quote_literal ($ 5) ||';' das ist keine ganze Zahl. Dies ist eine Zeichenfolge (varchar/text). Postgres haben Ihnen die gleiche Erklärung gegeben und normalerweise weisen Clients (wie PgAdmin) auf den genauen Fehlerort hin. –

Verwandte Themen