Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trigger - 2

Status
Not open for further replies.

rk68

Programmer
Jul 15, 2003
171
IN
Hi,

I have a trigger which fires on change in certain fields & inserts the old field data in a history table.
If the field has value (e.g.college name) & I replace it with null, a record is inserted with the member id in the history table but the old value (college name) is not inserted. This happens with any of fields. Please find below the code. This happens only if the field data is blank/null.
for e.g. if the college ADD1 has data "12th Street, Mumbai" & while updating the field, I blank it/remove "12th Street, Mumbai". So it has to update the history table with "12th Street, Mumbai" but it doesn't - inserts a record in the history table with member id. What changes do I need to do to get the old value updated in the history table.

-----------------------------------------------------
create or replace trigger "TGR_STUDENT_MST_CHG"
before Update or Insert of
MEMBER_ID,
COLLEGE_NAME,
COLLEGE_ADD1,
on T_STUDENT_MST
for each row

declare

VCOLLEGENAME t_student_mst.COLLEGE_NAME%type;
VCOLLEGEADD1 t_student_mst.COLLEGE_ADD1%type;

begin

if :)old.COLLEGE_NAME <> :new.COLLEGE_NAME) then
VCOLLEGENAME := :eek:ld.COLLEGE_NAME ;
else VCOLLEGENAME := null ;
end if;

if :)OLD.COLLEGE_ADD1 <> :NEW.COLLEGE_ADD1) then
VCOLLEGEADD1 := :OLD.COLLEGE_ADD1;
else VCOLLEGEADD1 := null ;
end if;

if updating then
insert into T_STUDENT_HST (
MEMBER_ID,
COLLEGE_NAME,
COLLEGE_ADD1)

values :)old.member_id,
VCOLLEGENAME,
VCOLLEGEADD1);

end if ;

end TGR_STUDENT_MST_CHG;
---------------------------------------------


TIA,
Raj
 
Just a guess here, but I would be very tempted to try:

[pre]
if :)OLD.COLLEGE_ADD1 <> :NEW.COLLEGE_ADD1)
[blue]OR :)NEW.COLLEGE_ADD1 IS NULL)[/blue] then
VCOLLEGEADD1 := :OLD.COLLEGE_ADD1;
else VCOLLEGEADD1 := null ;
end if;
[/pre]


---- Andy

There is a great need for a sarcasm font.
 
Great! I'm glad it works for you.
Like one of my teachers said: "Nothing equals to NULL, not even a NULL".
That's why equating or comparing anything with NULLs are tricky.


---- Andy

There is a great need for a sarcasm font.
 
This is how I would do it. Since you are only doing anything on update, do not fire on insert.
Code:
CREATE OR REPLACE TRIGGER "TGR_STUDENT_MST_CHG"
    BEFORE UPDATE OF Member_id, College_name, College_add1
    ON T_student_mst
    FOR EACH ROW

DECLARE
    Vcollegename   T_student_mst.College_name%TYPE;
    Vcollegeadd1   T_student_mst.College_add1%TYPE;
BEGIN
    Vcollegename := NULL;
    Vcollegeadd1 := NULL;

    IF NVL (:old.College_name, '  ') <> NVL (:new.College_name, '  ')
    THEN
        Vcollegename := :old.College_name;
    END IF;

    IF NVL (:old.College_add1, '  ') <> NVL (:new.College_add1, '  ')
    THEN
        Vcollegeadd1 := :old.College_add1;
    END IF;

    INSERT INTO T_student_hst (Member_id, College_name, College_add1)
         VALUES (:old.Member_id, Vcollegename, Vcollegeadd1);
END Tgr_student_mst_chg;

Bill
Lead Application Developer
New York State, USA
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top