2012-03-15 52 views
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() 
    ; 

回答

0

看來你被賦予OBX相關名 「testcode」 混淆自己:

TESTTABLES:

SET search_path='tmp'; 
    DROP TABLE "obx" CASCADE; 
    CREATE TABLE "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 
    ); 

DROP TABLE "testcode" CASCADE; 
CREATE TABLE "testcode" (
    "TcodeID" serial primary key, 
    "Analyzer" varchar, 
    "Parameter" varchar, 
    "TestName" varchar, 
    "ShortTestName" varchar, 
    "TestID" int4 
    ) ; 
DROP TABLE "finaldata" CASCADE; 
CREATE TABLE "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 
    ) ; 

功能& &觸發:

DROP FUNCTION testcode_matches() CASCADE; 
    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 ob."Pid", ob."Sid", ob."SidOrig", ob."Parameter", ob."Result", ob."ResultOrig", ob."Units" 
       , ob."OperatorID", ob."ObsTime", ob."MsgTime", ob."Analyzer" 
       , tc."TestName", tc."ShortTestName", tc."TestID" 
     FROM obx ob 
     JOIN testcode tc ON ob."Parameter" = tc."Parameter" 
     WHERE ob."Parameter" = NEW."Parameter" 
     -- AND ob."TestID" = NEW."TestID" -- Column does not exist 
     AND ob."obxID" = NEW."obxID" -- This appears to be the PK for obx 
      ; 
     RETURN NEW; 
    END; 
    $meat$ LANGUAGE plpgsql; 


    CREATE TRIGGER finaldata_ins 
    AFTER INSERT ON obx 
    FOR EACH ROW 
    EXECUTE PROCEDURE testcode_matches() 
    ; 

TESTDATA:

INSERT INTO testcode ("TestID", "Parameter") VALUES (101, 'Fudge'); 

INSERT INTO obx -- i"obxID" serial primary key, 
     ("Pid", "Sid" , "Parameter") VALUES ('Foo', 'Bar' ,'Fudge') ; 

SELECT * FROM finaldata; 

另外:我缺少一些外鍵約束。我期望測試數據至少有至少與候選鍵的obx相同的關鍵字段。 (加上som版本/日期,參數keyfield)看起來你的datamodel不適合支持你輸入的數據。

+0

WHERE ob。「Parameter」= NEW。「參數」 AND tc。「TestID」= NEW。「TestID」 – user1044111 2012-03-15 20:12:55

+0

>我缺少一些外鍵約束。我希望測試數據至少與候選鍵的obx具有相同的關鍵字段。 (加上索姆版本/日期,參數的關鍵字段) 請做任何建議 - 我在這裏知識的極限.. – user1044111 2012-03-15 20:29:23

+0

我改變了這一點,但現在得到一個錯誤 哪裏的「參數」=新。 「參數」 AND tc。「TestID」= NEW。「TestID」 [Err] ERROR:record「new」has no field「TestID」 – user1044111 2012-03-15 20:32:35

相關問題