Discussions
Categories
- 196.9K All Categories
- 2.2K Data
- 239 Big Data Appliance
- 1.9K Data Science
- 450.4K Databases
- 221.7K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 550 MySQL Community Space
- 478 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 546 SQLcl
- 4K SQL Developer Data Modeler
- 187.1K SQL & PL/SQL
- 21.3K SQL Developer
- 295.9K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.6K Development Tools
- 107 DevOps
- 3.1K QA/Testing
- 646K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 155 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 18 Java Essentials
- 160 Java 8 Questions
- 86K Java Programming
- 80 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 204 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 443 LiveLabs
- 38 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 171 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 232 Portuguese
Help Required on a Trigger!

657470
Member Posts: 3
Dear Fellows,
I am new to triggers. I am trying to write a trigger but continuously getting error messages on it when enabled.
Here is the scenario...
I have a table named 'MY_USERS' which has following structure:
Name Null? Type Default Value
----------------------------------------- -------- ---------------------------- --------------------------
USERID NOT NULL VARCHAR2(6)
PASSWORD NOT NULL VARCHAR2(10)
PWD_LASTMODIFIED_ON NOT NULL DATE SYSDATE
Now, I want that whenever someone will change password in the PASSWORD field, only the PWD_LASTMODIFIED_ON field in the same particular row, should automatically update its value.
I have written the following trigger for this:
CREATE OR REPLACE TRIGGER SMSUSER_PWD_LASTMODIFIED
AFTER UPDATE OF PASSWORD ON SMSUSER
FOR EACH ROW
BEGIN
UPDATE SMSUSER SET SMSUSER.PWD_LASTMODIFIED_ON=SYSDATE; +/*WHERE :new.PWD_LASTMODIFIED_ON <> :old.PWD_LASTMODIFIED_ON;*/+
END;
When I enable the commented WHERE clause in the trigger there is an error generated on update of PASSWORD field. Whereas, by commenting WHERE clause, when PASSWORD field is modified, PWD_LASTMODIFIED_ON field is updated in all rows.
Kindly help!
Regards
Edited by: SKAAT on Sep 1, 2008 10:58 AM
I am new to triggers. I am trying to write a trigger but continuously getting error messages on it when enabled.
Here is the scenario...
I have a table named 'MY_USERS' which has following structure:
Name Null? Type Default Value
----------------------------------------- -------- ---------------------------- --------------------------
USERID NOT NULL VARCHAR2(6)
PASSWORD NOT NULL VARCHAR2(10)
PWD_LASTMODIFIED_ON NOT NULL DATE SYSDATE
Now, I want that whenever someone will change password in the PASSWORD field, only the PWD_LASTMODIFIED_ON field in the same particular row, should automatically update its value.
I have written the following trigger for this:
CREATE OR REPLACE TRIGGER SMSUSER_PWD_LASTMODIFIED
AFTER UPDATE OF PASSWORD ON SMSUSER
FOR EACH ROW
BEGIN
UPDATE SMSUSER SET SMSUSER.PWD_LASTMODIFIED_ON=SYSDATE; +/*WHERE :new.PWD_LASTMODIFIED_ON <> :old.PWD_LASTMODIFIED_ON;*/+
END;
When I enable the commented WHERE clause in the trigger there is an error generated on update of PASSWORD field. Whereas, by commenting WHERE clause, when PASSWORD field is modified, PWD_LASTMODIFIED_ON field is updated in all rows.
Kindly help!
Regards
Edited by: SKAAT on Sep 1, 2008 10:58 AM
Best Answer
-
You don't need an UPDATE statement. Just assign
:new.pwd_last_modified_on := SYSDATE;
It should also be a <tt>BEFORE</tt> trigger.
btw you can format code with {noformat}{noformat} tags.
Answers
-
I'm not sure if I understand it.
If you want only PWD_LASTMODIFIED_ON to change i.e. PASSWORD should retain the old value you must act before update setting:
:NEW.PASSWORD := :OLD.PASSWORD and :NEW.PWD_LASTMODIFIED_ON := SYSDATE and let the update happen.
Regards
Etbin -
No, it is not like that.
Instead, If I execute an update statement such as...
update my_users set password='newpass' where userid='12345';
As soon as the old password is replaced by the new password, the PWD_LASTMODIFIED_ON filed should automatically be updated with the current SYSDATE.
The trigger should update that PWD_LASTMODIFIED_ON field automatically.
Edited by: SKAAT on Sep 1, 2008 11:23 AM -
You don't need an UPDATE statement. Just assign
:new.pwd_last_modified_on := SYSDATE;
It should also be a <tt>BEFORE</tt> trigger.
btw you can format code with {noformat}{noformat} tags.
-
SUCCESS!
Thanks A Million, William! -
At last, a forum point
-
When you modify the value of a table by a trigger on which the trigger is created you may hit Mutating table error. This article shows ways to over come the error.
Thanks,
Karthick.
This discussion has been closed.