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.

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top