Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7610237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:22:14+00:00 2026-05-31T01:22:14+00:00

A postgresql 9.1 DB writes data which is sent automatically from a machine. This

  • 0

A postgresql 9.1 DB writes data which is sent automatically from a machine. This works fine.

I have an AFTER INSERT …FOR EACH ROW trigger – this also works.
The trigger works by looking at another table and if there is a value in a field it fires.

I have two problems –
a. Some machines send more than 1 result. I am using LIMIT 1 to prevent duplicates and this means that I miss data from a machine that sends more than one result. What is an alternative to this?
b. Some machines share the same test code and I can`t find a way of qualifying this, to make it machine specific in the SELECT when I use the NEW value. Here is the code

CREATE FUNCTION testcode_matches()
    RETURNS TRIGGER as $$
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 /* various fields */
        SELECT /* various fields */ name, short, id
        from obx 
        WHERE "obx"."Parameter" = NEW."Parameter"
        LIMIT 1;
    END if;
    RETURN NEW;
END;
$$ language plpgsql;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T01:22:15+00:00Added an answer on May 31, 2026 at 1:22 am

    Plain-SQL version, without all the variable stuff. (unknown details omitted or guessed) (untested)

    CREATE FUNCTION testcode_matches()
        RETURNS TRIGGER as $meat$
    BEGIN
    
        INSERT INTO finaldata ( name, short, id /* various fields */ )
        SELECT tc.name, tc.shorttestname, tc.id /* various fields */
        FROM testcode tc
        JOIN obx ON obx.parameter = tc.parameter
        WHERE tc.parameter = NEW.parameter
        AND tc.id = NEW.id -- is this the PK for testcode ??
                    ;
        RETURN NEW;
    END;
    $meat$ language sql;
    

    UPDATE:

    set search_path='tmp';
    
    DROP TABLE obx CASCADE;
    CREATE TABLE obx
            ( parameter INTEGER NOT NULL PRIMARY KEY
            );
    
    DROP TABLE testcode CASCADE;
    CREATE TABLE testcode
            ( ID INTEGER NOT NULL PRIMARY KEY
            , parameter INTEGER NOT NULL  REFERENCES obx(parameter)
            , zname VARCHAR
            , shorttestname VARCHAR
            );
    
    DROP TABLE finaldata CASCADE;
    CREATE TABLE finaldata
            ( ID INTEGER NOT NULL PRIMARY KEY
            , zname VARCHAR
            , shorttestname VARCHAR
            );
    
    
    DROP FUNCTION testcode_matches();
    CREATE FUNCTION testcode_matches()
        RETURNS TRIGGER
        AS $meat$
    BEGIN
    
        INSERT INTO finaldata ( id, zname, shorttestname /* various fields */ )
        SELECT tc.id, tc.zname, tc.shorttestname /* various fields */
        FROM testcode tc
        JOIN obx ON obx.parameter = tc.parameter
        WHERE tc.parameter = NEW.parameter
        AND tc.id = NEW.id -- is this the PK for testcode ??
                    ;
        RETURN NEW;
    END;
    $meat$ language plpgsql;
    
    DROP TRIGGER testcode_ins;
    CREATE TRIGGER testcode_ins
            AFTER INSERT ON testcode
            FOR EACH ROW
            EXECUTE PROCEDURE testcode_matches()
            ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database in PostgreSQL which is named DATA in all caps. When
I currently have a system which required regular data imports. The db is Postgresql.
I have an application that can not afford to lose data, so Postgresql is
I downloaded PostgreSQL from the official website and ran the .dmg installer. After that
I need to replicate data from Microsoft SQL Server to MySQL or PostgreSQL. The
Right now I have a database (about 2-3 GB) in PostgreSQL, which serves as
I need to write a script that will output data from a PostgreSQL database
I have a PostgreSQL database with events. Each event has a datetime or an
I have postgresql running (/opt/local/lib/postgresql90/bin). The data base is set up @ /Users/demet8/postgres/data. I
I am trying to write data from a .csv file to my postgreSQL database.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.