PL/pgQSL - enumerating over ROWTYPE in stored procedure
After loading data from a csv into an existing Postgres table, I will have duplicate records that need to be merged. To do this, I plan on using a stored procedure that does the following:
current_record airports%ROWTPE;
new_record airports%ROWTYPE
FOR current_record in SELECT * from airports ORDER BY iko LOOP
SELECT * into (STRICT) new_record from airports where iko=current_record.iko AND oid < current_record.oid
-- IS IT POSSIBLE TO enumerate over each field as follows?
FOR int i = 0; i < new_rec开发者_开发百科ord.fields.count; i++ LOOP
IF new_record.field[i] IS NOT NULL THEN current_record.field[i] =
new_record.field[i]
END LOOP;
END LOOP;
Can I enumerate over fields using the FOR inner loop as shown above or will I have to hardcode my change logic for each field in the table? There are about 70 columns.
Other better ways on how to do this will help as well.
TIA.
There is no direct support for that. However you can query the system catalogs from within your function. But that won't make things faster.
Here is an example on how to do it in a trigger. That might get you started in the right direction.
http://wiki.postgresql.org/wiki/PL/pgSQL_Dynamic_Triggers
精彩评论