我試圖使用postgresql函數來啓動一個查詢對幾個表,然後做一個插入到另一個表,如果滿足條件。我每次都會在聲明或返回語句中發現錯誤。任何幫助表示讚賞。Postgresql for循環更新功能
基本上這應該:
- 循環對每行的health_alerts_triggered表,並利用現場數據然後查詢「healthEvents」表
如果在子查詢中的計數「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")
)
請提供health_alerts_config和healthaffiliatclientid的架構有一些數據 – Gab
添加你要的細節。謝謝。 – Eagledare