I defined the below plpgsql function (Before trigger) to update the data if one of the column provided , I will get the other one from static data. But I want to know what is the best way to get the static data for given "columna", I need to get the "columnb" from static_tbl (more like dictionary / map datastrcuture). At the moment I am querying inside the Trigger function on the static table .
I know that i can use the case statement as well instead of querying . Please let me know what will be the best fit here. Adding the test data Say ColumnA contains 'a' it will be propagated to ColumnB as 'Alphabet A' Otherwise ColumnB contains 'Alphabet A' it will be propagated to ColumnA as 'a' . What is the best way to store the static data used for conversion columnA value to columnB value?
CREATE FUNCTION update_tblname_column_b () RETURNS TRIGGER AS $$
BEGIN
IF NEW IS NULL THEN
RAISE EXCEPTION 'this function cannot be installed with a DELETE trigger';
END IF;
IF NEW.column_b IS NOT NULL AND NEW.column_a IS NULL THEN
NEW.column_a = (select column_a from static_tbl where column_b = NEW.column_b);
ELSIF NEW.column_a IS NOT NULL AND NEW.column_b IS NULL THEN
NEW.column_b = (select column_b from static_tbl where column_b = NEW.column_a);
END IF;
RETURN NEW;
END;
$$ LANGUAGE PLPGSQL;
CREATE TRIGGER populate_column
BEFORE INSERT OR UPDATE ON creatives FOR each ROW
EXECUTE PROCEDURE update_tblname_column_b();