Trigger that inserts automatic value in a record

Asked

Viewed 405 times

0

Good afternoon, guys. How are you? I’m trying to create a Rigger that when performing a partner registration it automatically marks that the partner is a client. In the database, this field is called CLIENT and receives the value’S', however, I am facing the following error when trying to register a partner :ORA-04098: 'TEST.AD_TRG_INC_UPD_TGFPAR' trigger is invalid and the revalidation failed. Could you help me?

CREATE OR REPLACE TRIGGER AD_TRG_INC_UPD_TGFPAR
BEFORE INSERT OR UPDATE OF CLIENTE ON TGFPAR
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW 
BEGIN
    IF :NEW.CLIENTE ='N' OR :NEW.CLIENTE IS NULL THEN 
       :NEW.CLIENTE := 'S';
END´´´
  • In this case the DEFAULT clause is not enough?

  • As well as DEFAULT clause?

  • https://oracle-base.com/articles/12c/default-values-for-table-columns-enhancements-12cr1#nulls alter table TGFPAR Modify default client’S';

3 answers

0

I believe you’re making a mess between UPDATE and INSERT, in case the field already exists you want to change it to’S', in case you have to use the UPDATE:

CREATE OR REPLACE TRIGGER AD_TRG_INC_UPD_TGFPAR
AFTER INSERT OR UPDATE OF CLIENTE ON TGFPAR
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW 
BEGIN
    IF :NEW.CLIENTE IS NULL THEN 
       :NEW.CLIENTE := 'S';
END

Another mistake that may be giving is that whenever you use the :NEW or :OLD the next value after . (dot) is the column name, check if it really is the column name of the value you want to change.

  • Oops, thank you very much friend. So really the name of the column is CUSTOMER. It comes with blank status, but for some procedures they need to be marked and the person has been forgetting. And as every partner receives this field marked by being a buyer or supplier, I want to put so that when registering a partner it is already marked. I’m starting to mess with triggers right now and I’m taking a leather rs

  • the same error occurred while trying the way you showed me. Is there something interfering to create this Rigger?

  • I made a code change the table name is TGFPAR and the column name CLIENT? If this is it can test, it will probably work.

  • I had already made that change and continued the same mistake. Is it because the partner is being inserted and it would be giving Insert and update in the same record?

  • I noticed that you used the BEFORE that is before, I believe you have to use the AFTER, it may be that the field does not exist yet so there is no way to do the UPDATE in a field that does not exist, I changed the code again, make a test

  • Gave the same error. In fact the field exists. If this field is not marked it receives null, the problem is that there is an error in the partner registration where employees forget to mark that the guy is a customer, there generates an error when making a sales order. When confirming the partner’s registration, this field must be auto filled. is a varchar2 field and receives’s value'

  • Still the same mistake?

  • Still the same mistake.

  • How are you creating Trigger? Post the full code for me to take a look

  • I edited the post with the last query in the database.

  • Where is the create Trigger?

  • I edited the code again. At the time of saving did not take that part there no, only the bottom line.

  • I changed it again, put the references

  • Gave the same, error. I think it is system failure already

Show 9 more comments

0

CREATE OR REPLACE TRIGGER AD_TRG_INC_UPD_TGFPAR
BEFORE INSERT OR UPDATE OF CLIENTE ON TGFPAR
FOR EACH ROW 
BEGIN
    IF TRIM(:NEW.CLIENTE) IS NULL THEN 
      :NEW.CLIENTE := 'S';
    END IF;
END;

You could also create a DEFAULT in the column

 ALTER TABLE TGFPAR
    MODIFY CLIENTE NOT NULL DEFAULT 'S';

Applying this to ensure there are no null values

UPDATE TGFPAR SET CLIENTE NOT NULL DEFAULT 'S'
WHERE TRIM(CLIENTE) IS NULL; 
  • Modify client that you passed will even help those who have already registered, but I need this field to be auto-filled when registering new partners. But Rigger is a good one. I’ll try it here, brother. Thanks for taking the time to help

  • The following error appeared: SQL Error [4084] [42000]: ORA-04084: cannot change NEW values for this type of trigger

  • Rigger has to be before.... Notepad stuff ...

  • Got it. But she’s Before and she still made that mistake.

  • Republica a a Trigger.

  • Whoa, sorry for the delay, buddy. I was a little tense about the service. I’ll republish yes.

  • I edited Trigger in the post, posted the last attempt

  • Wasn’t another Rigger "lost" from after setando new ? now it doesn’t make sense ...

  • So, I’m kind of trying every possible way, why can’t I make it work

Show 4 more comments

0

Good afternoon, I appreciate the help. I was able to solve thanks to your advice. The query was like this.

BEFORE INSERT OR UPDATE ON TGFPAR
--REFERENCING OLD AS OLD NEW AS NEW
FOR EACH ROW 
DECLARE
PRAGMA AUTONOMOUS_TRANSACTION; -- corrige mutante trigger
V_CLIENTE VARCHAR2(10);
BEGIN
SELECT user INTO V_CLIENTE
FROM DUAL;

:new.cliente:= 'S';
END;´´´ 

Browser other questions tagged

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