• Postgresql添加/删除触发器示例


    -- 定义触发器
    CREATE TRIGGER "vss_after_insert_flow_201702" AFTER INSERT ON "public"."vss_passenger_flow_201702"
    FOR EACH ROW
    EXECUTE PROCEDURE "vss_after_insert_flow"();
    --删除触发器
    DROP TRIGGER vss_after_insert_flow_201702 ON vss_passenger_flow_201702 ;
    --定义存储过程
    
    -- FUNCTION: public.vss_after_insert_flow()
    
    -- DROP FUNCTION public.vss_after_insert_flow();
    
    CREATE FUNCTION public.vss_after_insert_flow()
        RETURNS trigger
        LANGUAGE 'plpgsql'
        COST 100.0
        VOLATILE NOT LEAKPROOF 
    AS $BODY$
    BEGIN
        --Routine body goes here...
        IF EXISTS(SELECT camera_id FROM vss_passenger_hour 
            WHERE camera_id = NEW.i_camera_id and datetime = NEW.start_dt::DATE and EXTRACT(hour FROM time) = EXTRACT(hour FROM NEW.start_dt::TIME)) THEN
        UPDATE vss_passenger_hour 
            SET passenger_in_count = passenger_in_count + NEW.i_enter_num , passenger_out_count = passenger_out_count + NEW.i_leave_num 
                WHERE camera_id = NEW.i_camera_id and datetime = NEW.start_dt::DATE and EXTRACT(hour FROM time) = EXTRACT(hour FROM NEW.start_dt::TIME);
        ELSE
        INSERT INTO vss_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 TO postgres;
  • 相关阅读:
    英语八大时态
    linux yum被占用
    文件上传ajaxfileupload.js插件
    jquery.qrcode.js生成二维码
    判断手机,pc方式登录
    点击小图轮播,点击显示大图
    图片预览功能
    Java Hash表 数据结构
    Java Tree 树 数据结构
    Java Queue 队列
  • 原文地址:https://www.cnblogs.com/winkey4986/p/6407301.html
Copyright © 2020-2023  润新知