0

i need to update one column in the same table after inserting is done. INSERT already coded and cannot be changed . So we are going with TRIGGER approach to update one column value.

create or replace TRIGGER "BLT_TRIGGER" 
AFTER INSERT ON BLT_MAPPING 
FOR EACH ROW
BEGIN
UPDATE BLT_MAPPING 
SET FIELD_ID = (SELECT CASE WHEN CORR_TI = 'B' THEN '12345' ELSE '54321' END 
AS INSTITUTION FROM BROKER CO,LOAN LO
WHERE LO.CORR_ID = CO.CORR_ID AND LO.LOAN_ID = loanid_in_BLT_MAPPING_table 
whcih_got_inserted )
END;

I am getting purge error . What is the mistake i am doing in thins trigger .

3
  • Please show your error Commented Dec 1, 2018 at 1:06
  • INSERT cannot be changed? Why not? Commented Dec 1, 2018 at 3:41
  • Why not contemplate having a MERGE whereby INSERT and UPDATE are possible with WHERE conditions in a single statement; thus, there is a possibility of correcting the records. Besides, as rightly pointed out by @eaolson, triggers are obscure Commented Dec 1, 2018 at 6:06

1 Answer 1

2

Are you trying to update the same row that you just inserted? In that case, you want to make this a BEFORE INSERT trigger, and change :NEW.fieldid before it is inserted into the table. Something like this:

create or replace TRIGGER "BLT_TRIGGER" 
BEFORE INSERT ON BLT_MAPPING 
FOR EACH ROW
    l_new_fieldid     number; -- Or whatever the correct datatype is
BEGIN

    SELECT CASE WHEN CORR_TI = 'B' THEN '12345' ELSE '54321' END 
      INTO l_new_fieldid
      FROM BROKER CO,LOAN LO
     WHERE LO.CORR_ID = CO.CORR_ID 
       AND LO.LOAN_ID = :NEW.loanid;

    :NEW.fieldid := l_new_fieldid;

END;

But this is an inferior solution. Triggers obscure what you are trying to really do and are hard to debug when there is a problem. The better solution is to correct your INSERT statement with what you're trying to really do.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Its Working .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.