Subject | Re: Generate Incremental Strings |
---|---|
Author | Adam |
Post date | 2006-04-12T02:54:43Z |
> Currently, I am using the following trigger to generate anincremental ID,
>incremental
> create generator pmc_termid_gen;
> set generator pmc_termid_gen to 10000;
>
> create trigger create_pmc_termid for pmc_index_list
> before insert position 0
> as begin
> new.termid = gen_id(pmc_termid_gen, 1);
> end !!
>
> I am wondering if it is possible change this to generate
> alphabets? Something likeGenerators return '64 bit integers'.
>
> create generator pmc_termid_gen;
> set generator pmc_termid_gen to 'aaaaa';
I am not familiar with incremental alphabets, what the carry rules
are etc.
What happens when you increment 'z', is it meant to jump to 'aa' etc.
Firstly, if you are talking about your primary key, it is the wrong
way to go about it. A primary key should have no real world
significance. If it does, create a surrogate key instead.
If however you just want to generate a unique 5 character string for
some other purpose, then perhaps you could write a UDF to convert
to 'base 26'. You could pass the generator value into the UDF and
retrieve the varchar result.
Courtesy Wikipedia (slightly modified to base 26)
This function is javascript, it would not take a brain surgeon to
convert it to whatever language you are comfortable with and write it
as a dll.
function toBase26(d) {
var r = d % 26;
if(d-r==0) {return toChar(r);}
else {return toBase26( (d-r)/26)+toChar(r);}
}
function toChar(n) {
var alpha = "abcdefghijklmnopqrstuvwxyz";
return alpha.charAt(n);
}
Adam