`
lovelease
  • 浏览: 382700 次
社区版块
存档分类
最新评论

postgresql产生随机数和随机日期的存储过程

sql 
阅读更多
--function to get random number=============================================================
-- DROP FUNCTION IF EXISTS get_random_number(integer, integer);
CREATE OR REPLACE FUNCTION get_random_number(integer, integer) RETURNS integer AS
$BODY$
DECLARE
	start_int ALIAS FOR $1;
	end_int ALIAS FOR $2;
BEGIN
	RETURN trunc(random() * (end_int-start_int + 1) + start_int);
END;
$BODY$
LANGUAGE plpgsql;
--产生1-10之间的随机数(包括边缘)
--SELECT get_random_number(1, 10);

--function for get random date between start_date and end_date
-- DROP FUNCTION IF EXISTS get_random_date(date, date);
CREATE OR REPLACE FUNCTION get_random_date(start_date date, end_date date) RETURNS integer AS
$BODY$
DECLARE
	interval_days integer;
	random_days integer;
	random_date date;
BEGIN
	interval_days := end_date - start_date;
	random_days := get_random_number(0, interval_days);
	random_date := start_date + random_days;
	RETURN date_part('year', random_date) * 10000 + date_part('month', random_date) * 100 + date_part('day', random_date);
END;
$BODY$
LANGUAGE plpgsql;
-- SELECT get_random_date('2000-01-01', '2013-12-31');--result:20100902
分享到:
评论
1 楼 zhengyong7232 2015-09-14  
Create or replace function test_function(b_count integer) returns integer as
$body$
DECLARE
BEGIN
while b_count>0 loop
INSERT INTO vis_call_records(call_records_id, call_long_num, called_long_num, sender, receiver, call_start_time,
call_stop_time, is_connect, connect_time, is_manager_caller,device_type, is_receipt)
VALUES (b_count, '10010100102','10000000101', '1-1-1-102','森哥_PC','2015-09-07 10:21:45','2015-09-07 10:24:21', 1, 150, 0, 1, 1);
b_count:=b_count-1;
end loop;
RETURN b_count;
END;
$body$ LANGUAGE plpgsql;

相关推荐

Global site tag (gtag.js) - Google Analytics