-- defining triggers CREATE TRIGGER INSERT on "public". vss_passenger_flow_201702 " for each ROWEXECUTEPROCEDURE " Vss_after_ Insert_flow "();
-- Delete Trigger DROP TRIGGER on vss_passenger_flow_201702;
--Defining stored Procedures--FUNCTION:public.vss_after_insert_flow ()--DROP FUNCTION public.vss_after_insert_flow ();CREATE FUNCTION Public. Vss_after_insert_flow ()RETURNS TriggerLANGUAGE'Plpgsql' Cost100.0VOLATILE notleakproof as$BODY $BEGIN --Routine body goes here ... IF EXISTS(SELECTcamera_id fromVss_passenger_hourWHEREcamera_id=new.i_camera_id and datetime =NEW.START_DT::D ate andEXTRACT (Hour fromTime=EXTRACT (Hour fromNew.start_dt::time)) Then UPDATEVss_passenger_hourSETPassenger_in_count=Passenger_in_count+New.i_enter_num, Passenger_out_count=Passenger_out_count+New.i_leave_numWHEREcamera_id=new.i_camera_id and datetime =NEW.START_DT::D ate andEXTRACT (Hour fromTime=EXTRACT (Hour fromnew.start_dt::time); ELSE INSERT intoVss_passenger_hour (region_id,camera_id,datetime, Time,passenger_in_count,passenger_out_count)VALUES(New.i_area_id,new.i_camera_id,new.start_dt,to_timestamp (To_char (NEW.START_DT,'YYYY-MM-DD HH24')||': 00:00','YYYY-MM-DD HH24:MI:SS'), new.i_enter_num,new.i_leave_num); END IF; RETURN NULL;END$BODY $;ALTER FUNCTION Public. Vss_after_insert_flow () OWNER toPostgres
PostgreSQL Add/Remove triggers sample