1. 程式人生 > >postgresql使用動態語句實現函數

postgresql使用動態語句實現函數

exce 語句 sta reg andro count send proc 驗證碼

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使用動態語句實現函數