Skip to main content
add test part
Source Link
J. Monticolo
  • 15.9k
  • 1
  • 29
  • 65

You have to create a trigger.

Test and trigger creation below :

CREATE-- ORDROP REPLACETABLE TRIGGERIF trigger_nomenEXISTS public.nomen;
-- DROP TABLE BEFOREIF INSERTEXISTS ORpublic.nomenclature;
CREATE UPDATETABLE ONpublic.nomen (nomen SMALLINT, geom GEOMETRY(POINT, 2154));
CREATE TABLE public.nomenclature FOR(nomenclature EACHSMALLINT, ROWgeom GEOMETRY(POLYGON, 2154));

-- Trigger part
CREATE BEGINOR REPLACE FUNCTION func_trigger_nomen()
  RETURNS trigger AS
$$
BEGIN
     NEW.nomen = (SELECT poly.nomenclature
     FROM nomenclature poly
     WHERE ST_CONTAINS(poly.geom, NEW.geom)
     LIMIT 1);
     RETURN NEW;
END;
$$
LANGUAGE 'plpgsql';

CREATE TRIGGER trigger_nomen
 END; BEFORE INSERT OR UPDATE ON public.nomen
  FOR EACH ROW
  EXECUTE PROCEDURE func_trigger_nomen();
-- End of trigger part

INSERT INTO public.nomenclature (nomenclature, geom) VALUES (42, ST_GeomFromText('POLYGON((0 0, 0 2, 2 2, 2 0, 0 0))'));
INSERT INTO public.nomen (geom) VALUES (ST_GeomFromText('POINT(1 1)'));
SELECT * FROM public.nomen;

You have to create a trigger :

CREATE OR REPLACE TRIGGER trigger_nomen
   BEFORE INSERT OR UPDATE ON nomen
   FOR EACH ROW

   BEGIN

     NEW.nomen = (SELECT poly.nomenclature
     FROM nomenclature poly
     WHERE ST_CONTAINS(poly.geom, NEW.geom)
     LIMIT 1);
     RETURN NEW;

   END;

You have to create a trigger.

Test and trigger creation below :

-- DROP TABLE IF EXISTS public.nomen;
-- DROP TABLE IF EXISTS public.nomenclature;
CREATE TABLE public.nomen (nomen SMALLINT, geom GEOMETRY(POINT, 2154));
CREATE TABLE public.nomenclature (nomenclature SMALLINT, geom GEOMETRY(POLYGON, 2154));

-- Trigger part
CREATE OR REPLACE FUNCTION func_trigger_nomen()
  RETURNS trigger AS
$$
BEGIN
     NEW.nomen = (SELECT poly.nomenclature
     FROM nomenclature poly
     WHERE ST_CONTAINS(poly.geom, NEW.geom)
     LIMIT 1);
     RETURN NEW;
END;
$$
LANGUAGE 'plpgsql';

CREATE TRIGGER trigger_nomen
  BEFORE INSERT OR UPDATE ON public.nomen
  FOR EACH ROW
  EXECUTE PROCEDURE func_trigger_nomen();
-- End of trigger part

INSERT INTO public.nomenclature (nomenclature, geom) VALUES (42, ST_GeomFromText('POLYGON((0 0, 0 2, 2 2, 2 0, 0 0))'));
INSERT INTO public.nomen (geom) VALUES (ST_GeomFromText('POINT(1 1)'));
SELECT * FROM public.nomen;
fix to prevent cycle
Source Link
J. Monticolo
  • 15.9k
  • 1
  • 29
  • 65

You have to create a trigger :

CREATE OR REPLACE TRIGGER trigger_nomen
   BEFORE INSERT OR UPDATE ON nomen
   FOR EACH ROW

   BEGIN

     UPDATE nomen SET NEW.nomen = (SELECT poly.nomenclature
     FROM nomenclature poly
     WHERE ST_CONTAINS(poly.geom, NEW.geom)
     LIMIT 1);
     RETURN NEW;

   END;

You have to create a trigger :

CREATE OR REPLACE TRIGGER trigger_nomen
   BEFORE INSERT OR UPDATE ON nomen
   FOR EACH ROW

   BEGIN

     UPDATE nomen SET NEW.nomen = poly.nomenclature
     FROM nomenclature poly
     WHERE ST_CONTAINS(poly.geom, NEW.geom);

   END;

You have to create a trigger :

CREATE OR REPLACE TRIGGER trigger_nomen
   BEFORE INSERT OR UPDATE ON nomen
   FOR EACH ROW

   BEGIN

     NEW.nomen = (SELECT poly.nomenclature
     FROM nomenclature poly
     WHERE ST_CONTAINS(poly.geom, NEW.geom)
     LIMIT 1);
     RETURN NEW;

   END;
fix to prevent cycle
Source Link
J. Monticolo
  • 15.9k
  • 1
  • 29
  • 65

You have to create a trigger :

CREATE OR REPLACE TRIGGER trigger_nomen
   AFTERBEFORE INSERT OR UPDATE ON nomen
   FOR EACH ROW

   BEGIN

     UPDATE nomen SET NEW.nomen = poly.nomenclature
     FROM nomenclature poly
     WHERE ST_CONTAINS(poly.geom, nomenNEW.geom);

   END;

You have to create a trigger :

CREATE OR REPLACE TRIGGER trigger_nomen
   AFTER INSERT OR UPDATE ON nomen
   FOR EACH ROW

   BEGIN

     UPDATE nomen SET nomen = poly.nomenclature
     FROM nomenclature poly
     WHERE ST_CONTAINS(poly.geom, nomen.geom);

   END;

You have to create a trigger :

CREATE OR REPLACE TRIGGER trigger_nomen
   BEFORE INSERT OR UPDATE ON nomen
   FOR EACH ROW

   BEGIN

     UPDATE nomen SET NEW.nomen = poly.nomenclature
     FROM nomenclature poly
     WHERE ST_CONTAINS(poly.geom, NEW.geom);

   END;
Source Link
J. Monticolo
  • 15.9k
  • 1
  • 29
  • 65
Loading