Need help with PK [message #80756] |
Sun, 17 November 2002 00:23  |
Aydah
Messages: 11 Registered: November 2002
|
Junior Member |
|
|
How to increment the primary key using pre-insert trigger in block level, I have tried it but it didn’t’ work the pk is trainer_no and it is varchar2, I want the number to be followed by T
So the first primary key is T0001 and the second must be T0002 and so on,
Here is my code:
declare
max_trainerno tbl_Trainers.TRAI_TRAINER_NO%Type;
trainernumber char;
begin
select max(to_number(substr(TRAI_TRAINER_NO,2,3)))
into max_trainerno
from tbl_Trainers;
max_trainerno:=max_trainerno+1;
trainernumber:='T'||max_trainerno;
/*message (The id for this trainer is:||to_char(max_trainerno));'*/
:tbl_trainers.TRAI_TRAINER_NO := max_trainerno;
end;
can I use a format mask along with the code?
Please help me it is my first time writing to you guys! don’t turn me down
Cheers,
Aydah
|
|
|
Re: Need help with PK [message #80758 is a reply to message #80756] |
Mon, 18 November 2002 02:21  |
Shiju Joy
Messages: 4 Registered: February 2002
|
Junior Member |
|
|
Hi,
You don't need to write so much code.
All you got to do is to create a sequence Eg
create sequence seq_trainernumber;
In Pre Insert Trigger, just use one command -
select 'T'||seq_trainernumber.nextval into :tbl_trainers.TRAI_TRAINER_NO from dual
Bye
Shiju
|
|
|