随机生成编码(判断重复)(oracle函数)

2015-11-21 01:39:28 · 作者: · 浏览: 2

?

create or replace function generate_coupons_code(v_length in number:=16) return varchar2 is
  v_code varchar2(70);
  type t_arr is varray(16) of varchar2(1);
  char_arr t_arr;
  flag number(1,0):= 0;
begin
  char_arr:=t_arr('0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f');
  for i in 1..v_length  
  loop  
      v_code:=v_code||char_arr(floor(dbms_random.value(1,17)));
      --dbms_output.put_line(floor(dbms_random.value(1,17)));
  end loop;
  select count(1) into flag from b_coupons c WHERE c.code = v_code;
  if flag >
0 then return generate_coupons_code(v_length); end if; return(v_code); end generate_coupons_code;