Postgresql – Column to be populated using other columns values

postgresql

I would like to add a new column in my table using SQL code in PostgreSQL / PostGIS, the values in this column must be filled from the other eight columns and updated with a trigger as I insert data to a new line for the first rows. As I show in the attached image.

Table with the new column

I have used this code to alter the table,

ALTER TABLE table_name ADD COLUMN NEW_COLUMN varchar;

UPDATE table_name SET NEW_COLUMN = "PP" || '.' || "MM" || '.' || "DD" || '.' || "BB" || '.' || "QQ" || '.' || "LL" || '.' || "ID"::varchar || '.' || "UU";

But I am having a problem using this trigger because it does not accept:

CREATE OR REPLACE FUNCTION update_en() 
RETURNS TRIGGER AS 
$$ 
BEGIN 
NEW.codigo :=; 
RETURN NEW; 
END; 
$$ 
LANGUAGE plpgsql; 
CREATE TRIGGER update_en 
BEFORE UPDATE OR INSERT ON public."EDIFICACAO_4" 
FOR EACH ROW 
EXECUTE PROCEDURE update_en();

Best Answer

The question on why you think you need that aside, you can simplify the concatenation using concat_ws()which you can easily use in the trigger as well:

CREATE OR REPLACE FUNCTION update_en() 
RETURNS TRIGGER AS 
$$ 
BEGIN 
  NEW.codigo := concat_ws('.', new."PP", new."MM", new."DD", new."BB", new."QQ", new."LL", new."ID", new."UU");
  RETURN NEW; 
END; 
$$ 
LANGUAGE plpgsql;