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

  • SEARCH
  • Home
  • 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 6547369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:49:22+00:00 2026-05-25T11:49:22+00:00

I have the following PostgreSQL script: CREATE OR REPLACE FUNCTION merge_fields() RETURNS VOID AS

  • 0

I have the following PostgreSQL script:

    CREATE OR REPLACE FUNCTION merge_fields() RETURNS VOID AS $$
     DECLARE
        current_record airport%ROWTYPE;
        new_record airport%ROWTYPE;
        column_def RECORD;
        old_value TEXT;
        new_value TEXT;
        field_name TEXT;
        sql_text  TEXT;
        integer_var INT;

     BEGIN       

        FOR current_record in SELECT * FROM airport LOOP

        -- Match Record based on iko and modified time
          SELECT * INTO new_record FROM airport WHERE 
              iko = current_record.iko AND mod_time > current_record.mod_time;             
          IF FOUND THEN 
            FOR column_def IN 

                    -- Get fields for this record
                SELECT ordinal_position, column_name, data_type 
                FROM information_schema.columns  
                WHERE 
                        table_schema = 'public' 
                AND     table_name = 'airport' 
                ORDER BY ordinal_position
                LOOP                                                

                    field_name := column_def.column_name;
                    IF ((field_name = 'gid') OR (field_name = 'mod_time')) THEN                             
                    ELSE    

-- Get each field value for current and new record.  New record is matched record.                                  
                        EXECUTE 'SELECT ($1).' || field_name || '::' || column_def.data_type INTO old_value USING current_record;
                        EXECUTE 'SELECT ($1).' || field_name || '::' || column_def.data_type INTO new_value USING new_record;                                       

                        IF new_value IS NOT NULL THEN               
                            IF new_value <> old_value THEN

                            sql_text := 'UPDATE ' || 'airport' 
                                    || ' SET '
                                    || quote_ident(field_name)
                                    || ' = ' 
                                    || quote_literal(new_value) 
                                    || ' WHERE gid = '
                                    || current_record.gid;


    -- Set current record field value same as new record field value
                                      EXECUTE 'UPDATE ' || 'airport' 
                                    || ' SET '
                                    || quote_ident(field_name)
                                    || ' = ' 
                                    || quote_nullable(new_value) 
                                    || ' WHERE gid = '
                                    || current_record.gid;

                            GET DIAGNOSTICS integer_var = ROW_COUNT;                        
                            RAISE NOTICE E'Old Value\t   rows affected: %\t ',integer_var;  

                                RAISE NOTICE E'Old Value\t   name: %\t   value: %.\n',          
                                    field_name,         
                                    old_value;                                                      
                                RAISE NOTICE E'New Value\t   name: %\t   value: %.\n',          
                                    field_name,         
                                    new_value;                                                                  

                            END IF;
                        END IF;

                    END IF; 

                -- End column enumerating loop
                 END LOOP;  


          END IF;
          IF NOT FOUND THEN        
          END IF;

        END LOOP;    

        EXCEPTION
            WHEN TOO_MANY_ROWS THEN
                     RAISE NOTICE E'Too many records match search criteria.';   
            WHEN OTHERS THEN
           --     RAISE EXCEPTION 'airports % not found';              
     END;
    $$
    LANGUAGE plpgsql;

What I am trying to do is merge two records in a database table based on the modified time.
What script does is as follows:

For each record in table, I find all matching records by a key field named “iko” with
modified time later than the record current record.

There will one or no matches. If a match is found, I enumerate each field in the current and matching record and synchronize the fields if the latter is not null.

Script runs as expected with no errors. Also, the diagnostic result ROW_COUNT indicates 1 row is updated After the EXECUTE command in the script is called. However, when I refresh the table, I do not see the expected change.

Any ideas why?

TIA.

  • 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-25T11:49:22+00:00Added an answer on May 25, 2026 at 11:49 am

    Changes were being made to table successfully as indicated but due to a bug in the script, where I was trying to assign non-TEXT data types to a TEXT variable, exception was being thrown, causing transaction to be rolled back. This is where the script is failing if the record field value is not TEXT.:

    -- Get each field value for current and new record.  New record is matched record.                                  
                            EXECUTE 'SELECT ($1).' || field_name || '::' || column_def.data_type INTO old_value USING current_record;
                            EXECUTE 'SELECT ($1).' || field_name || '::' || column_def.data_type INTO new_value USING new_record;  
    

    where old_value and new_value are TEXT variables declared earlier in the script.

    If the column name is known, one can declare a variable such as

     var_name table_name.column_name%TYPE; 
    

    which would dynamically hold any data type. Because, column name is being discovered dynamically such a variable cannot be used. I could not think of a way to easily achieve this so I chose a completely different strategy.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following script to create a table: -- Create State table. DROP
I have the following table in PostgreSQL 8.4.5: snake=> create table gps ( id
I am using Postgresql 8.3 and have the following simple function that will return
I have following string String str = replace :) :) with some other string;
I have following text in a file 23456789 When I tried to replace the
I have following classes. class A { public: void fun(); } class B: public
I have an existing database(Postgresql). How can i create models from it? How can
I have the following situation. I have a PHP script that imports a CSV
I'm trying to create a FUNCTION in my Postgres database from a Bash script.
I have the following problem, I need to put in a script that is

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.