2017-03-13 64 views
0

我试图使用postgresql函数来启动一个查询对几个表,然后做一个插入到另一个表,如果满足条件。我每次都会在声明或返回语句中发现错误。任何帮助表示赞赏。Postgresql for循环更新功能

基本上这应该:

  1. 循环对每行的health_alerts_triggered表,并利用现场数据然后查询“healthEvents”表
  2. 如果在子查询中的计数“healthEvents”返回更大应该在health_alerts_triggered表中执行零行和插入操作。

    CREATE OR REPLACE FUNCTION sp_alertcheck() 
    RETURNS SETOF record; 
    DECLARE r record; 
    FOR r IN SELECT * 
        FROM health_alerts_config 
    LOOP 
    INSERT INTO health_alerts_triggered (health_affiliate_id, alert_format,alert_minutes, health_client_internal_id, alert_triggered_date, alert_processed_date, alert_id, affiliate_name, client_name) 
    VALUES (r.health_affiliate_id, r.alert_format, r.alert_minutes, r.health_client_internal_id, now() as alert_triggered_date, '' as alert_processed_date, id, ha.health_affiliate_description, hc.health_client_description) 
    WHERE (SELECT COUNT(*) from "heatlhEvents" he 
        WHERE he.format = r.format, 
        AND he.healthaffiliatclientid = r.health_affiliate_description, 
        AND he.timestamp > Now() - r.minutes 
        ) 
        > 0 
    INNER JOIN health_affiliates ha 
    ON r.health_affiliate_id = ha.id 
    INNER JOIN health_clients hc 
    ON r.health_client_internal_id = hc.id 
    END LOOP; 
    RETURN result; 
    $BODY$ 
    LANGUAGE plpgsql; 
    

架构为health_alerts_config:

CREATE TABLE public.health_alerts_config 
(
    id bigint NOT NULL DEFAULT nextval('"healthAlerts_id_seq"'::regclass), 
    health_affiliate_id bigint NOT NULL, 
    alert_format character varying(10) COLLATE pg_catalog."default" NOT NULL, 
    alert_minutes integer NOT NULL, 
    health_client_internal_id bigint NOT NULL, 
    CONSTRAINT health_alerts_pkey PRIMARY KEY (id) 
) 

样品行:1,1,ADT,60,1

模式为health_affiliates

CREATE TABLE public.health_affiliates 
(
    id bigint NOT NULL, 
    health_affiliate_description character varying(50) COLLATE pg_catalog."default" NOT NULL, 
    CONSTRAINT health_affiliates_pkey PRIMARY KEY (id) 
) 

样品行: 1,'TestCo'

架构为health_clients

CREATE TABLE public.health_clients 
(
    id integer NOT NULL DEFAULT nextval('"healthClients_id_seq"'::regclass), 
    health_affiliate integer NOT NULL, 
    health_client_id character varying(20) COLLATE pg_catalog."default" NOT NULL, 
    health_client_description character varying(100) COLLATE pg_catalog."default" NOT NULL, 
    CONSTRAINT id_pk PRIMARY KEY (id) 
) 

样品行:1,1,200, 'TestCoClient'

模式为 “healthEvents”

CREATE TABLE public."healthEvents" 
(
    "ID" integer NOT NULL DEFAULT nextval('"healthEvents_ID_seq"'::regclass), 
    "fileName" character varying(100) COLLATE pg_catalog."default" NOT NULL, 
    "instanceName" character varying(20) COLLATE pg_catalog."default" NOT NULL, 
    "channelName" character varying(50) COLLATE pg_catalog."default" NOT NULL, 
    affiliate character varying(20) COLLATE pg_catalog."default" NOT NULL, 
    "fileSizeKB" bigint, 
    "beginProcessing" timestamp without time zone, 
    "endProcessing" timestamp without time zone, 
    resubmission boolean NOT NULL, 
    destination integer NOT NULL, 
    "insertTime" timestamp without time zone NOT NULL DEFAULT now(), 
    "messageID" bigint NOT NULL, 
    direction integer NOT NULL, 
    "affiliateClient" character varying(40) COLLATE pg_catalog."default" NOT NULL, 
    "messageType" character varying(15) COLLATE pg_catalog."default", 
    "messageStatus" integer, 
    "messageCode" character varying(10) COLLATE pg_catalog."default", 
    "insertDate" date DEFAULT ('now'::text)::date, 
    "affiliateClientID" character varying(50) COLLATE pg_catalog."default", 
    CONSTRAINT "PK" PRIMARY KEY ("ID") 

+0

请提供health_alerts_config和healthaffiliatclientid的架构有一些数据 – Gab

+0

添加你要的细节。谢谢。 – Eagledare

回答

0

我不我认为你甚至需要一个循环。我想这样做(未经测试):

INSERT INTO health_alerts_triggered (
    health_affiliate_id, alert_format, alert_minutes, health_client_internal_id, 
    alert_triggered_date, alert_processed_date, alert_id, 
    affiliate_name, client_name 
) 
SELECT 
    r.health_affiliate_id, r.alert_format, r.alert_minutes, r.health_client_internal_id, 
    now() as alert_triggered_date, '' as alert_processed_date, id, 
    ha.health_affiliate_description, hc.health_client_description 
FROM 
    health_alerts_config r 
    INNER JOIN health_affiliates ha 
    ON r.health_affiliate_id = ha.id 
    INNER JOIN health_clients hc 
    ON r.health_client_internal_id = hc.id 
WHERE 
    exists(
    SELECT 1 
    FROM "heatlhEvents" he 
    WHERE he.format = r.format, 
    AND he.healthaffiliatclientid = r.health_affiliate_description, 
    AND he.timestamp > now() - r.minutes 
) 
; 
+0

看起来很有希望,我会给它一个镜头。谢谢回复! – Eagledare

+0

这工作,谢谢! – Eagledare