postgresql使用動態語句實現函數

來源:互聯網
上載者:User

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.