Jump to content
Fórum Script Brasil
  • 0

Trigger altera registro mesma tabela


Kellyton Armelin
 Share

Question

Bom dia,

Tenho uma tabela chamada "pcpcpr" a qual tenho dois campos, um é o "pcpr_statuss_1" varchar(1) e o outro é "status_op" varchar (1), como faço para criar um trigger que ao alterar o campo "pcpr_statuss_1" para 'C', ele altere com o mesmo valor o campo "status_op"?

Eu fiz assim mas não deu certo:

Essa é a função

CREATE OR REPLACE FUNCTION f_au_pcpcpr()
  RETURNS trigger AS
$BODY$
BEGIN
  IF NEW.pcpr_statuss_1 = 'C' THEN
      NEW.status_op = 'C';
  END IF;
RETURN NEW;
END; $BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION f_au_pcpcpr()
  OWNER TO postgres;

E aqui está a trigger:

CREATE TRIGGER tr_au_pcpcpr
  AFTER UPDATE
  ON pcpcpr
  FOR EACH ROW
  EXECUTE PROCEDURE f_au_pcpcpr();

Porém, quando eu executo a alteração me dá erro.

 

Abs

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

boa noite, você tem que usar ":=" para mudar o campo status_op, 

CREATE OR REPLACE FUNCTION f_au_pcpcpr() RETURNS trigger AS $$
BEGIN
  IF NEW.pcpr_statuss_1 = 'C' THEN
      NEW.status_op := 'C';
  END IF;
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share



  • Forum Statistics

    • Total Topics
      151k
    • Total Posts
      649.1k
×
×
  • Create New...