序列组合字符串和数字

时间:2012-06-22 03:58:54

标签: oracle plsql sequence

  

可能重复:
  is it possible to have alphanumeric sequence generator in sql

如何在Oracle中编写一个函数来生成一个字符串,如A0001,A0002,.. A9999,... AA0001,AA9999,... AZ0001,AZ9999,...作为员工ID。

我从这个逻辑开始,但我只能生成A001到Z999:

CREATE OR REPLACE procedure GLOBALSHIFTER.p1 ( v1 in varchar2 default 'A') IS 

   id varchar2(4); 
   p1 number(2) := ascii(substr(v1,1,1)); 
   p2 number(2); 
   p3 number(2) ; 
   c number(1) :=1; 

begin 

   select s1.nextval into p2 from dual; 

   If p2 < 1000 then 
      id:= concat(chr(p1), lpad(p2,3,0)); 
   ELSE 
      select s1.nextval into p2 from dual; 
      p1 := p1 + 1; 
      id := concat(chr(p1), lpad(p2,3,0)); 
   END IF; 

   insert into t1 values(id); 
   commit; 

end;

0 个答案:

没有答案