码迷,mamicode.com
首页 > 数据库 > 详细

postgresql使用动态语句实现函数

时间:2018-01-05 16:07:07      阅读:345      评论:0      收藏:0      [点我收藏+]

标签:bst   select   func   port   vol   err   sms   lsb   var   

CREATE FUNCTION "public"."NewProc"(IN "p_date" varchar, OUT "out_vv" int4)
RETURNS "pg_catalog"."int4" AS $BODY$
declare
v_sqlcode INTEGER;
v_date date;
v_pre_date date;
v_sqlerrm varchar(4000);
v_begintime varchar(20);
v_endtime varchar(20);
v_sql varchar(4000);
activations INTEGER;
pro_name varchar(4000);
v_column INTEGER;
intro_pv INTEGER;
intro_uv INTEGER;
cm_pv INTEGER;
cm_uv INTEGER;
rp_pv INTEGER;
rp_uv INTEGER;
cp_pv INTEGER;
cp_uv INTEGER;
registers INTEGER;
begin
v_date :=to_date(p_date,‘yyyymmdd‘);
v_pre_date :=v_date+1;
v_begintime:=to_char(now(),‘yyyy/mm/dd hh24:mi:ss‘);
pro_name:=‘sp_stat_client_visit‘;

--连接远程数据库
PERFORM dblink_connect_u(‘test_dblink‘,‘dbname=redfinger host=10.100.0.254 port=7432 user=redfinger password=redfinger‘);
--获取到安卓的激活量
--v_sql:=‘select count(*) from rf_version_log t ‘;
v_sql:=‘select count(*) from rf_version_log_tmp t where t.install_type = ‘‘1‘‘ and client_type=‘‘android‘‘
and t.create_time >=‘||quote_literal(v_date)||‘ and t.create_time < ‘||quote_literal(v_pre_date)||‘‘;
select * from dblink(‘test_dblink‘,v_sql)as t(total_num int) into activations;

GET DIAGNOSTICS v_column = ROW_COUNT;

v_endtime:=to_char(now(),‘yyyy/mm/dd hh24:mi:ss‘);

--记录日志
insert into T_LOG(pro_name,tj_date,begin_time,end_time,err_infor,run_result,v_sql,sql_count)
values(pro_name,p_date,v_begintime,v_endtime,v_sqlerrm,‘Y‘,v_sql,v_column);

-- 将激活的用户的所有的cuid查询出来并且保存到临时表 lsb_active_info
delete from lsb_active_info;
v_sql :=‘select distinct cuid from rf_version_log_tmp where install_type=‘‘1‘‘ and create_time >=‘||quote_literal(v_date)||‘ and create_time < ‘||quote_literal(v_pre_date)||‘ and client_type=‘‘android‘‘‘;
insert into lsb_active_info(cuid)
select * from dblink(‘test_dblink‘,v_sql) as t(cuid VARCHAR);

GET DIAGNOSTICS v_column = ROW_COUNT;
v_endtime:=to_char(now(),‘yyyy/mm/dd hh24:mi:ss‘);
insert into T_LOG(pro_name,tj_date,begin_time,end_time,err_infor,run_result,v_sql,sql_count)
values(pro_name,p_date,v_begintime,v_endtime,v_sqlerrm,‘Y‘,v_sql,v_column);

--将访问了引导页的cuid保存到cuid_tmp临时表中,然后与激活量的cuid表进行表连接,查询访问引导页的pv和uv type_id=1
v_sql:=‘select count(i.cuid) pv,count(distinct i.cuid) uv from lsb_active_info i,lsb_cbh_test b
where i.cuid = b.cuid and b.type_id=1‘;
execute v_sql into intro_pv,intro_uv;
GET DIAGNOSTICS v_column = ROW_COUNT;
v_endtime:=to_char(now(),‘yyyy/mm/dd hh24:mi:ss‘);
insert into T_LOG(pro_name,tj_date,begin_time,end_time,err_infor,run_result,v_sql,sql_count)
values(pro_name,p_date,v_begintime,v_endtime,v_sqlerrm,‘Y‘,v_sql,v_column);

-- 统计访客访问云手机页面的的pv和uv type_id=2
v_sql:=‘select count(i.cuid) pv,count(distinct i.cuid) uv from active_info_tmp i,lsb_cbh_test b
where i.cuid = b.cuid and b.type_id=2‘;
execute v_sql into cm_pv,cm_uv;
GET DIAGNOSTICS v_column = ROW_COUNT;
v_endtime:=to_char(now(),‘yyyy/mm/dd hh24:mi:ss‘);
insert into T_LOG(pro_name,tj_date,begin_time,end_time,err_infor,run_result,v_sql,sql_count)
values(pro_name,p_date,v_begintime,v_endtime,v_sqlerrm,‘Y‘,v_sql,v_column);

-- 统计访问注册页面的pv和uv type_id=3
v_sql:=‘select count(i.cuid) pv,count(distinct i.cuid) uv from active_info_tmp i,lsb_cbh_test b
where i.cuid = b.cuid and b.type_id=3‘;
execute v_sql into rp_pv,rp_uv;
GET DIAGNOSTICS v_column = ROW_COUNT;
v_endtime:=to_char(now(),‘yyyy/mm/dd hh24:mi:ss‘);
insert into T_LOG(pro_name,tj_date,begin_time,end_time,err_infor,run_result,v_sql,sql_count)
values(pro_name,p_date,v_begintime,v_endtime,v_sqlerrm,‘Y‘,v_sql,v_column);

--统计短信验证码的pv和uv
v_sql :=‘select count(t.sms_mobile),count(distinct t.sms_mobile) from rf_sms t where t.sms_source = ‘‘3‘‘ and client_source=‘‘android‘‘
and t.send_time >= ‘||quote_literal(v_date)||‘ and t.send_time < ‘||quote_literal(v_pre_date)||‘‘;
select * from dblink(‘test_dblink‘,v_sql)as t(cp_pv int,cp_uv int) into cp_pv,cp_uv;

GET DIAGNOSTICS v_column = ROW_COUNT;

v_endtime:=to_char(now(),‘yyyy/mm/dd hh24:mi:ss‘);

--记录日志
insert into T_LOG(pro_name,tj_date,begin_time,end_time,err_infor,run_result,v_sql,sql_count)
values(pro_name,p_date,v_begintime,v_endtime,v_sqlerrm,‘Y‘,v_sql,v_column);

--统计用户数
v_sql :=‘select count(*) from rf_user t where t.create_time>= ‘||quote_literal(v_date)||‘ and t.create_time < ‘||quote_literal(v_pre_date)||‘ and client = ‘‘android‘‘‘;
select * from dblink(‘test_dblink‘,v_sql)as t(registers int) into registers;

GET DIAGNOSTICS v_column = ROW_COUNT;

v_endtime:=to_char(now(),‘yyyy/mm/dd hh24:mi:ss‘);

--记录日志
insert into T_LOG(pro_name,tj_date,begin_time,end_time,err_infor,run_result,v_sql,sql_count)
values(pro_name,p_date,v_begintime,v_endtime,v_sqlerrm,‘Y‘,v_sql,v_column);

--插入结果数据
delete from lsb_stat_client_visit_temp where stat_date=v_date;
v_sql:=‘insert into lsb_stat_client_visit_temp(activations,intro_pv,intro_uv,cm_pv,cm_uv,rp_pv,rp_uv,stat_date,cp_pv,cp_uv,registers) values($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11)‘;
execute v_sql using activations,intro_pv,intro_uv,cm_pv,cm_uv,rp_pv,rp_uv,v_date,cp_pv,cp_uv,registers;
GET DIAGNOSTICS v_column = ROW_COUNT;
v_endtime:=to_char(now(),‘yyyy/mm/dd hh24:mi:ss‘);
insert into T_LOG(pro_name,tj_date,begin_time,end_time,err_infor,run_result,v_sql,sql_count)
values(pro_name,p_date,v_begintime,v_endtime,v_sqlerrm,‘Y‘,v_sql,v_column);

out_vv:=0;

PERFORM dblink_disconnect(‘test_dblink‘);

EXCEPTION
WHEN OTHERS THEN

v_sqlerrm :=substr(sqlerrm, 1, 2000);

insert into T_LOG(pro_name,tj_date,begin_time,err_time,err_infor,run_result,v_sql,sql_count)
values(pro_name,p_date,v_begintime,NOW(),v_sqlerrm,‘N‘,v_sql,v_column);
PERFORM dblink_disconnect(‘test_dblink‘);
out_vv :=-1;
end;
$BODY$
LANGUAGE ‘plpgsql‘ VOLATILE COST 100
;

ALTER FUNCTION "public"."NewProc"(IN "p_date" varchar, OUT "out_vv" int4) OWNER TO "sys_report";

 

postgresql使用动态语句实现函数

标签:bst   select   func   port   vol   err   sms   lsb   var   

原文地址:https://www.cnblogs.com/phoneber/p/8203491.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!