-- Function: randomstring(character varying, integer)
-- DROP FUNCTION randomstring(character varying, integer);
CREATE OR REPLACE FUNCTION randomstring(IN vi_oprt character varying, IN vi_stringlength integer, OUT vo_string character varying)
RETURNS character varying AS
$BODY$
/* *******************************
* desc:The type of variable vi_stringtype including U or u(upper),L or l(lower),A or a(upper and lower)
* vi_stringlength : the length of string that returned
********************************* */
declare
vn_stringtype varchar(2);
vn_returnstring varchar(20);
vn_post integer;
vn_substr varchar(1);
vn_loopcnt integer;
begin
vn_stringtype := lower(vi_oprt);
if vn_stringtype = 'u' then
SELECT
array_to_string(array( SELECT chr(ascii('A') + floor(random() * 26)::integer) as srandom
FROM generate_series(1, vi_stringlength) ),'') as t into vn_returnstring;
elsif vn_stringtype = 'l' then
SELECT
array_to_string(array( SELECT chr(ascii('a') + floor(random() * 26)::integer) as srandom
FROM generate_series(1, vi_stringlength) ),'') as t into vn_returnstring;
else
SELECT
array_to_string(array( SELECT chr(ascii('a') + floor(random() * 26)::integer) as srandom
java生成随机数的方法FROM generate_series(1, vi_stringlength) ),'') as t into vn_returnstring;
-- gained the recycle count
for i in 1 .. ceil(random()*vi_stringlength) loop
vn_post := ceil(random()*vi_stringlength);
vn_substr := upper(substring(vn_returnstring,vn_post,1));
vn_returnstring := coalesce(substring(vn_returnstring,1,vn_post-1),'') || vn_substr || coalesce(substring(vn_returnstring,vn_post+1),'');
end loop;
end if;
vo_string := vn_returnstring;
return;
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论