Condition check to launch Trigger

Asked

Viewed 139 times

0

I have this table:

CREATE TABLE public.tbtramite
(
  idtramite integer NOT NULL DEFAULT nextval('tbtramite_idtramite_seq'::regclass),
  dsdescricao character varying(255),
  dttramite timestamp without time zone,
  stnotificacao character varying(1),
  stretornada character varying(1),
  sttramitepublico character varying(1),
  idencaminhamento integer NOT NULL,
  idunidadeenvio integer NOT NULL,
  idusuarioemissor integer,
  idusuarioreceptor integer,
  CONSTRAINT tbtramite_pkey PRIMARY KEY (idtramite),
  CONSTRAINT fk_1pybfodhvhni89th78wu28av3 FOREIGN KEY (idunidadeenvio)
      REFERENCES public.tbunidade (idunidade) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT fk_7njc4ihx8teld84auv7b83cht FOREIGN KEY (idusuarioemissor)
      REFERENCES public.tbusuario (idusuario) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT fk_986qxqfo4nx3e6chji0q7mymg FOREIGN KEY (idusuarioreceptor)
      REFERENCES public.tbusuario (idusuario) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT fk_iqha0kxcqwwfweibygg4p91xu FOREIGN KEY (idencaminhamento)
      REFERENCES public.tbencaminhamento (idencaminhamento) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
  OIDS=FALSE
);
ALTER TABLE public.tbtramite
  OWNER TO postgres;

And I have a Rigger who executes every time a piece of data is entered in this table.

However, I would like Trigger to be executed only when inserting the new column data idusuarioreceptor were NULL and the field of idunidadeenvio were !=(different) from 1.

Is it possible to perform this check when launching Trigger? Or make a "light" process that checks and is then call the heavier process?

To Trigger:

CREATE TRIGGER acerto_de_caminho_trigger
AFTER INSERT ON tbtramite
FOR EACH ROW
EXECUTE PROCEDURE acerto_de_caminho();
  • put the function and Trigger code

  • I do not see the need to put the codes because my doubt is not in the function and functioning of it, but if one could make another and this call it. Or a condition check for Rigger’s own drive.

  • But in any case Trigger is there, the function I am still developing, but does not enter the scope of the question

  • IF (NEW.value <> 1 or new.value null) I did one like this once: CREATE OR REPLACE FUNCTION public.atualiza_impressora()&#xA;RETURNS trigger&#xA;AS&#xA;$$&#xA;begin&#xA;&#xA;IF (NEW.descricao <> OLD.descricao or new.codigoempresa <> old.codigoempresa)&#xA;THEN&#xA;UPDATE impressoras&#xA;SET codigoempresa = new.codigoempresa, setorinstalacao = new.descricao&#xA;where codigo = new.codigoimpressora; &#xA;&#xA;END IF;&#xA;return null;&#xA;end&#xA;$$ LANGUAGE plpgsql I hope it helps you

  • At Function, that’s not for you?

  • @R.Santos serve, serve. But every function call is highly costly for the bank, in which case at each insertion it will call a function and only inside it will check

  • @R.Santos what I would like to do is to perform this separate function check, only loading the function if the conditions were set

  • 1

    Doesn’t that help you? https://answall.com/questions/220238/realizar-condi%C3%A7%C3%A3o-na-Trigger

  • @R.Santos and as help, tomorrow I will test, but I believe it will solve my problem

  • @R.Santos despite this ai is in Mysql, twisting pro postgres behave alike

  • Hopefully, you won’t forget about Feedback alone

Show 6 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.