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";

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

时间: 2024-11-08 19:57:30

postgresql使用动态语句实现函数的相关文章

【转】Oracle 执行动态语句

1.静态SQLSQL与动态SQL Oracle编译PL/SQL程序块分为两个种:其一为前期联编(early binding),即SQL语句在程序编译期间就已经确定,大多数的编译情况属于这种类型:另外一种是后期联编(late binding),即SQL语句只有在运行阶段才能建立,例如当查询条件为用户输入时,那么Oracle的SQL引擎就无法在编译期对该程序语句进行确定,只能在用户输入一定的查询条件后才能提交给SQL引擎进行处理.通常,静态SQL采用前一种编译方式,而动态SQL采用后一种编译方式.

c++第十二章 -(动态管理内存、动态数组和函数返回动态内存)

1.静态内存,编译时申请,存储在栈,如基本数据类型. 2.动态内存由一些没有名字,只有地址的内存块构成.那些内存块是在程序运行期间动态分配的.它们来自一个标准c++库替你管理的“大池子”(内存池),从内存池申请一些内存需要用new语句,他将根据你提供的数据类型分配一块大小适当的内存.你不必担心内存块的尺寸问题. 3.注意在用完内存块之后,应该用delete语句把它返还内存池.另外作为一种附加的保险措施,在释放内存之后还要把关联的指针置NULL,对空指针进行“解引用”会报错. class Comp

理论+实操:shell之case语句for/while/until循环语句、函数、数组-满满的干货

前言: 单引号表示纯符号 用双引号表字符串 case多分支语句 循环语句 for循环语句 while循环语句 until循环语句 shell函数 shell数组 一 : case语句的结构 1.1 case结构 多分支表现形式:if语句,case语句 case 语句可以使用正则表达式,正则表达式后面会去阐述 针对变量的不同取值,分别执行不同的命令程序 case 中::表示一句话的结束 如果匹配某一个固定值,大多数用caes,其适用于某一个固定值 如果匹配某一个区间范围,大多数用if else 语

javascript循环语句及函数

循环语句 1.While 语法: while (exp) { //statements; } 说明: while (变量<=结束值) { 需执行的代码 } 例1: var i = 1; while(i < 3){ alert(i); i++; } 注意:如果忘记增加条件中所用变量的值,该循环永远不会结束.这可能导致浏览器崩溃. ( 用while输出1-1000的数值 var i=1; while(i<=1000){ if(i%2){ document.write(i); } //输出1-

动态内存分配函数与释放函数

(1)分配函数 void *malloc(sizeof(unsigned size) 功能:在内存的动态存储区中分配一块长度位size的连续空间. 返回值:指针,存放被分配内存的起始地址.若未申请到空间,则返回NULL(0). void *:指向任何类型的数据,在使用时,要进行强制类型转换. 例如:int *malloc(sizeof(int)) struct student *malloc(sizeof(struct student)) (2)释放函数 void free(void *ptr)

动态语句SQL语句写法

/****************************************************************************************************************************************************** 动态语句语法:exec/sp_executesql语法 整理人:中国风(Roy) 日期:2008.06.06 *******************************************

PLSQL_动态语句的解析(概念)

2014-06-02 BaoXinjian 1. 最简单例子 (1).SQL 1 DECLARE 2 3 l_sql_text VARCHAR (1000); 4 5 TYPE c_type_wip_entity IS REF CURSOR; 6 7 c_wip_entity c_type_wip_entity; 8 9 r_wip_entity wip_entities%ROWTYPE; 10 11 BEGIN 12 13 l_sql_text :='select * from wip_ent

动态调用DLL函数有时正常,有时报Access violation的异常

动态调用DLL函数有时正常,有时报Access violation的异常 typedef int (add *)(int a,int b); void test() { hInst=LoadLibraryA("aimdtl.dll"); (FARPROC &)add=GetProcAddress(hInst,"add"); add(1,2); } 按这个代码执行,add函数有时OK,有时报Access violation的异常.看到提示,第一反应就是内存异常

ibatis动态语句加and 和不加and

<select id="queryGoodsGroupCodeName" parameterClass="String" resultClass="codeTextBean"> SELECT L4_GDS_GROUP_CD CODE,L4_GDS_GROUP_DESC NAME FROM DM_GDS_GROUP_INF_TD <dynamic prepend="WHERE"> <isNotEmp