have issue using substring with postgres trigger here is the trigger i used
CREATE FUNCTION TRIGGER1() RETURNS trigger AS $autouuid$
BEGIN
update test_points.scada_rtu i
set dummy #field_name
= upper(substr (NEW.city_name, 1, 2) || right (NEW.city_name, 1) || '-' || substr (NEW.phase_name, 1, 2) || right (NEW.phase_name, 1) || '-' || substr (NEW.area_name, 1, 3) || '-' ||
substr(NEW.name, 1, 2) || right (NEW.name, 1) || '-' || substr (NEW.rtu_model, 1, 2) || right (NEW.rtu_model, 1))
WHERE i.id = OLD.id;
RETURN NEW;
END;
$autouuid$ LANGUAGE plpgsql;
CREATE TRIGGER autouuid_update BEFORE INSERT OR UPDATE ON test_points.scada_rtu
FOR EACH ROW
EXECUTE PROCEDURE public.TRIGGER1();
CREATE FUNCTION TRIGGER1() RETURNS trigger AS $autouuid$
BEGIN
NEW.dummy=upper(
concat_ws('-',
regexp_replace(NEW.city_name, '^(..).*(.)$', '\1\2'),
regexp_replace(NEW.phase_name, '^(..).*(.)$', '\1\2'),
regexp_replace(NEW.name, '^(..).*(.)$', '\1\2'),
regexp_replace(NEW.rtu_model, '^(..).*(.)$', '\1\2')
)
);
RETURN NEW;
END;
$autouuid$ LANGUAGE plpgsql;
CREATE TRIGGER autouuid_update BEFORE INSERT OR UPDATE ON test_points.scada_rtu
FOR EACH ROW
EXECUTE PROCEDURE public.TRIGGER1();
NEW
record directly, without issuing a new update.record
-type variable NEW
will solve your problem, because it'll operate within trigger's context where trigger's special variables are visible. Your error is caused by the fact your update query thinks it's a reference to one of the target table's columns, ignoring the variable. @Adrian Klaver||
. concat_ws()
does both of these things for you, also ignoring NULL
values.regexp_replace()
.#comment
is not a valid PL/pgSQL comment. Use --comment
for single-line end-of-line or /*comment*/
for multi-line, mid-line comments. Your comments will be saved with the function definition and visible whenever someone looks it up.