0
三個Postgresql表(Windows V 9.1.3) 表obx是一個動態表,它接收來自多臺機器的數據。 我們想要產生一個after插入觸發函數 表testcode是一個固定表,它包含我們試圖匹配obx表的值。如果testcode TestID的整數字段不爲空,我們希望使用表obx中的後插入觸發器將數據插入到新的表finaldata中。Postgresql觸發器功能正在工作,但正在生成重複項
第一個觸發器起作用,但它會產生重複數據。觸發器必須包含「LIMIT 3」,因爲其中一臺機器每次發送三個結果。 試用僅使用SQL的第二個選項,但不起作用。
表finaldata中標記爲XY的字段供內部使用。
CREATE TABLE "public"."obx" (
"obxID" serial primary key,
"Pid" varchar,
"Sid" varchar,
"SidOrig" varchar,
"Parameter" varchar,
"Result" varchar,
"ResultOrig" varchar,
"Units" varchar,
"RefRange" varchar,
"Flag" varchar,
"FlagOrig" varchar,
"OperatorID" varchar,
"ObsTime" char(14),
"MsgTime" char(14),
"UnixTime" int4,
"Analyzer" varchar,
"Segment" varchar
)
;
CREATE TABLE "public"."testcode" (
"TcodeID" serial primary key,
"Analyzer" varchar,
"Parameter" varchar,
"TestName" varchar,
"ShortTestName" varchar,
"TestID" int4
)
;
CREATE TABLE "public"."finaldata" (
"FdataID" serial primary key,
"Pid" varchar,
"Sid" varchar,
"SidOrig" varchar,
"Parameter" varchar,
"Result" varchar,
"ResultOrig" varchar,
"Units" varchar,
"OperatorID" varchar,
"ObsTime" varchar,
"MsgTime" varchar,
"Analyzer" varchar,
"TestName" varchar,
"ShortTestName" varchar,
"TestID" varchar,
"XYchar1" varchar,
"XYchar2" varchar,
"XYchar3" varchar,
"XYint1" int4,
"XYint2" int4,
"XYint3" int4,
"XYGuid" uuid
)
;
DECLARE
var INTEGER;
name text;
short text;
id integer;
BEGIN
SELECT count("TestID") from testcode WHERE "testcode"."Parameter" = NEW."Parameter" into var;
IF var > 0 THEN
SELECT "TestName", "ShortTestName", "TestID" from testcode where "Parameter" = NEW."Parameter" Limit 1 into name, short, id;
INSERT INTO finaldata ("Pid", "Sid", "SidOrig", "Parameter", "Result", "ResultOrig", "Units", "OperatorID", "ObsTime", "MsgTime", "Analyzer", "TestName", "ShortTestName", "TestID")
SELECT "Pid", "Sid", "SidOrig", "Parameter", "Result", "ResultOrig", "Units", "OperatorID", "ObsTime", "MsgTime", "Analyzer", name, short, id
from obx WHERE "obx"."Parameter" = NEW."Parameter"
LIMIT 3;
END if;
RETURN NEW;
END;
----------------------------
-- Triggers structure for table "public"."obx"
-- ----------------------------
CREATE TRIGGER finaldata_ins
AFTER INSERT ON obx
FOR EACH ROW
EXECUTE PROCEDURE testcode_matches()
;
CREATE FUNCTION testcode_matches()
RETURNS TRIGGER AS $meat$
BEGIN
INSERT INTO finaldata ("Pid", "Sid", "SidOrig", "Parameter", "Result", "ResultOrig", "Units", "OperatorID", "ObsTime", "MsgTime", "Analyzer", "TestName", "ShortTestName", "TestID")
SELECT "obx"."Pid", "obx"."Sid", "obx"."SidOrig", "obx"."Parameter", "obx"."Result", "obx"."ResultOrig", "obx"."Units", "obx"."OperatorID", "obx"."ObsTime", "obx"."MsgTime", "obx"."Analyzer", "TestName", "ShortTestName", "TestID"
FROM obx testcode
JOIN obx ON "obx"."Parameter" = "testcode"."Parameter"
WHERE "testcode"."Parameter" = NEW."Parameter"
AND "testcode"."TestID" = NEW."TestID"
;
RETURN NEW;
END;
$meat$
LANGUAGE plpgsql;
----------------------------
-- Triggers structure for table "public"."obx"
-- ----------------------------
CREATE TRIGGER finaldata_ins
AFTER INSERT ON obx
FOR EACH ROW
EXECUTE PROCEDURE testcode_matches()
;
WHERE ob。「Parameter」= NEW。「參數」 AND tc。「TestID」= NEW。「TestID」 – user1044111 2012-03-15 20:12:55
>我缺少一些外鍵約束。我希望測試數據至少與候選鍵的obx具有相同的關鍵字段。 (加上索姆版本/日期,參數的關鍵字段) 請做任何建議 - 我在這裏知識的極限.. – user1044111 2012-03-15 20:29:23
我改變了這一點,但現在得到一個錯誤 哪裏的「參數」=新。 「參數」 AND tc。「TestID」= NEW。「TestID」 [Err] ERROR:record「new」has no field「TestID」 – user1044111 2012-03-15 20:32:35