Question

im trying to insert in my db a 4 charactes password. It is generated by a sequence... the problem im having is that i cant find a way to make that happend. I just find out about triggers, i know the answer to my problem might be there and this is what i have till now :S

CREATE OR REPLACE TRIGGER TRG_PASSWORD 
BEFORE INSERT OR UPDATE OF PASSWORD ON USER
FOR EACH ROW

BEGIN
     SELECT SEQ_PASSWORD.NEXTVAL
     INTO new.PASSWORD := LPAD(:new.PASSWORD,4,'0');
     FROM DUAL;
END;

Im new with all these database stuff! plz help! and sorry for my english btw! i know its awful!

Was it helpful?

Solution

Odd password rules as it'd be relatively easy to guess a new user's... but you can do this:

 SELECT LPAD(SEQ_PASSWORD.NEXTVAL, 4, '0')
 INTO :new.PASSWORD
 FROM DUAL;

Or from 11g:

:new.PASSWORD := LPAD(SEQ_PASSWORD.NEXTVAL, 4, '0');

As an alternative to LPAD you could also use TO_CHAR:

:new.PASSWORD := TO_CHAR(SEQ_PASSWORD.NEXTVAL, 'FM0000');

Of course, all of these assume your USER.PASSWORD field is defined as varchar2, not as number.

You probably don't want the 'OF PASSWORD' clause though; that means the sequence will only be used to set the password if that field is set by the insert or update statement, which doesn't seem very intuitive; and resetting the password on update of anything seems unhelpful too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top