postgresql使用动态语句实现函数

Posted 我是东风破

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了postgresql使用动态语句实现函数相关的知识,希望对你有一定的参考价值。

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使用动态语句实现函数的主要内容,如果未能解决你的问题,请参考以下文章

Greenplum(PostgreSql)函数实现批量删除表

PostgreSQl 函数返回多个动态结果集

如何在函数的 UPDATE 或 SELECT 语句中使用动态列名?

请问 postgresql中有 replace into或者 Merge into 这样的功能的语句吗 我想实现这样的功能啊

尝试使用 if 语句创建函数时出现 Postgresql 语法错误

将表传递给 postgreSQL 函数,执行 select 语句,返回表