Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...The forum looks great! You guys have done a fantastic job on arranging things there...Your site is very precise and fun to visit..."

Geography

Where in the world do Tek-Tips members come from?
benipal (IS/IT--Management)
29 Oct 09 9:58
Hi
I want to capture the system date into 'modifydate' field  in my 'employee' table whenever there is change in the value of the 'address' field of the 'employee' table.

Looking for a trigger for this task.
I am using Oracle 9i
Thank you
SantaMufasa (TechnicalUser)
29 Oct 09 10:50
Benipal,

Tek-Tips suggests that we do not provide simply "coding services" in response to posts. We're happy to help you with code that you have produced. If you can post your attempt at creating such a trigger (after having consulted your Oracle Triggers references), even if it has errors or is non-functional, we'll be happy to offer suggestions to help complete the task.

santaMufasa
(aka Dave of Sandy, Utah, USA)
[I provide low-cost, remote Database Administration services: www.dasages.com]
"Beware of those that seek to protect you from harm or risk. The cost will be your freedoms and your liberty."

benipal (IS/IT--Management)
29 Oct 09 13:25
create or replace trigger modifyemployee
after update of address on employee
begin
update employee set modifydate=sysdate

// i can't figure out the code to go over here

end;

thanks
SantaMufasa (TechnicalUser)
29 Oct 09 13:37
Benipal,

Here is your trigger code. (Notice that you want a BEFORE UPDATE trigger so that it will include the new contents of "modifydate" in the newly updated row. Also, you want the trigger to fire "FOR EACH ROW", which allows you to access both the ":new." and ":old." values for each row that you are updating.):

CODE

create or replace trigger modifyemployee
    before update of address on employee
    for each row
begin
    :new.modifydate := sysdate;
end;
/

Trigger created.

SQL> update employee set address = 'xyz' where empnum = 10201;

1 row updated.

SQL> select address,modifydate from employee where empnum = 10201;

ADDRESS    MODIFYDAT
---------- ---------
xyz        29-OCT-09

1 row selected.
Let us know if this is what you wanted.

santaMufasa
(aka Dave of Sandy, Utah, USA)
[I provide low-cost, remote Database Administration services: www.dasages.com]
"Beware of those that seek to protect you from harm or risk. The cost will be your freedoms and your liberty."

benipal (IS/IT--Management)
29 Oct 09 15:24
hi Mufasa
I am using an application to insert/update record in table.
The same SAVE button is used to create or update employee record.
When I change and save the address, it shows the updated address but modifydate field still NULL.
I see no error message while executing trigger.
Please advise,
SantaMufasa (TechnicalUser)
29 Oct 09 16:02
Benipal,

You should be able to test the trigger by running an address UPDATE in SQL*Plus. Let us know those results.

Also, since your [Save] button affects both INSERT and UPDATE, then modify your trigger code to read:

CODE

before insert or update ...

Let us know the outcomes.

santaMufasa
(aka Dave of Sandy, Utah, USA)
[I provide low-cost, remote Database Administration services: www.dasages.com]
"Beware of those that seek to protect you from harm or risk. The cost will be your freedoms and your liberty."

Beilstwh (Programmer)
30 Oct 09 9:54
Also dont forget, the trigger will only fire if the ADDRESS is changed. If any other column is changed, then NO update will occur. Personally I would set the modify date when anything gets changed by using

create or replace trigger modifyemployee
    before update or insert on employee
    for each row
begin
    :new.modifydate := sysdate;
end;
/

Bill
Lead Application Developer
New York State, USA

benipal (IS/IT--Management)
3 Nov 09 7:17
I have a test database which is a copy of the live database.
I am trying the following scripts on test database.

ALTER TABLE
EMPLOYEE
add (who_created varchar(30), when_created date );


ALTER TABLE
EMPLOYEE
modify (who_created default user, when_created default sysdate );


ALTER TABLE
EMPLOYEE
add (who_modified varchar(30) default null, when_modified date default null );


CREATE OR REPLACE TRIGGER modifyemployee
BEFORE  UPDATE OF address ON employee
when (NEW.address != OLD.address)
FOR EACH ROW
BEGIN
    
 :NEW.when_modified := sysdate;
    :NEW.who_modified := user;

END;

When I run these scripts on test database, the trigger doesn't work.
I also get the error message in live database( related to when_modified field) even though I ran the scripts on test database.
Please advise
Thank you,

 
Beilstwh (Programmer)
3 Nov 09 10:20
Your column names are wrong. Use


CREATE OR REPLACE TRIGGER modifyemployee
BEFORE  INSERT OR UPDATE OF address ON employee
when (NEW.address != NVL(OLD.address,'@@@'))
FOR EACH ROW
BEGIN
    
 :NEW.when_created  := sysdate;
 :NEW.who_created := user;

END;

Bill
Lead Application Developer
New York State, USA

benipal (IS/IT--Management)
3 Nov 09 10:49
Hi Bill
Address is never NULL and I want to update the who_modified and when_modified fields with user and sysdate whenever there is change in address value.
 
Beilstwh (Programmer)
3 Nov 09 12:45
on an insert the old.address will always be null.

Bill
Lead Application Developer
New York State, USA

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close