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

Linux中定时执行DB2的存储过程

时间:2016-04-27 17:01:28      阅读:316      评论:0      收藏:0      [点我收藏+]

标签:

一、需求说明

  将DB2中的实时数据按天和小时汇总到汇总表中。

 

二、过程及步骤

  第一步:编写DB2存储过程。

  1. 在Dbvisualizer中新建SQL editor。

  2. 代码:

 1 --/
 2 CREATE PROCEDURE "NCPOLLDATA"."AGGREGATE_ENTITY_DAILY"
 3 ()
 4 LANGUAGE SQL
 5 SPECIFIC SQL160427123206300
 6 BEGIN
 7   --declare varibles
 8   DECLARE V_PREFIX_TIME VARCHAR(7);
 9   DECLARE V_START_TIME VARCHAR(16);
10   DECLARE V_END_TIME VARCHAR(16);
11   DECLARE V_AGGREGATE_TIMESTAMP BIGINT DEFAULT 0;
12   DECLARE V_YEAR_VALUE INTEGER DEFAULT 0;
13   DECLARE V_MONTH_VALUE INTEGER DEFAULT 0;
14   DECLARE V_AGGREGATE_TIME TIMESTAMP;
15   --set varibles value
16   SET V_PREFIX_TIME = (1 || REPLACE(SUBSTR(CHAR(current_timestamp - 1 DAYS), 3, 9), -, ‘‘));
17   SET V_START_TIME = V_PREFIX_TIME || 000000000;
18   SET V_END_TIME = V_PREFIX_TIME || 235959999;
19   SET V_YEAR_VALUE = YEAR(current_timestamp - 1 DAYS);
20   SET V_MONTH_VALUE = MONTH(current_timestamp - 1 DAYS);
21   SET V_AGGREGATE_TIME = current_timestamp - 1 DAYS;
22   SET V_AGGREGATE_TIMESTAMP = TIMESTAMPDIFF(2, CHAR(current_timestamp - TIMESTAMP(1970-01-01-00.00.00)));
23   --action
24   FOR TEMP_CUR AS 
25     SELECT t1.MONITOREDOBJECTID AS "POLICY_ID", t2.MAINNODEENTITYID AS "ENTITY_ID", 
26     MIN(VALUE) AS "MIN_VALUE", MAX(VALUE) AS "MAX_VALUE",  AVG(BIGINT(VALUE)) AS "AVG_VALUE", 
27     COUNT(VALUE) AS "DATA_COUNT", SUM(BIGINT(VALUE)) AS "DATA_SUM" 
28     FROM POLLDATA t1 LEFT JOIN MONITOREDINSTANCE t2 ON t1.MONITOREDINSTID = t2.MONITOREDINSTID 
29     WHERE t1.TDWTIME BETWEEN V_START_TIME AND V_END_TIME 
30     GROUP BY t1.MONITOREDOBJECTID, t2.MAINNODEENTITYID 
31     ORDER BY t1.MONITOREDOBJECTID, t2.MAINNODEENTITYID
32   DO 
33   --insert statement
34     INSERT INTO POLLDATA_ENTITY_DAILY 
35     VALUES (TEMP_CUR.POLICY_ID, TEMP_CUR.ENTITY_ID, V_AGGREGATE_TIMESTAMP, 
36       V_YEAR_VALUE, V_MONTH_VALUE, V_AGGREGATE_TIME, 
37       TEMP_CUR.MIN_VALUE, TEMP_CUR.MAX_VALUE, TEMP_CUR.AVG_VALUE, TEMP_CUR.DATA_COUNT, TEMP_CUR.DATA_SUM); 
38   END FOR;
39 END
40 /

  注意:在Dbvisualizer中编写DB2存储过程,第一行的 --/  和最后一行的 / 是必不可少的否则报错。

  3. 执行存储过程: 点击Dbvisualizer的存储过程编辑框的执行按钮即可,也可以在命令行中执行call procedure_name。

 

  第二步:编写可在Linux系统执行的脚本命令。

#!/bin/sh

DB2_HOME=/opt/IBM/tivoli/netcool/platform/linux2x86/db2

PATH=$DB2_HOME/bin:$PATH

export $DB2_HOME

export $PATH

startTime=`date +%Y%m%d%H%M%S`

echo $startTime

echo "开始执行脚本"

db2 connect to ITNM user NCIM using tiv0li

echo "连接上数据库,开始执行脚本"

db2 "call NCPOLLDATA.AGGREGATE_ENTITY_DAILY()"

endTime=`date +%Y%m%d%H%M%S`

echo $endTime

echo "脚本执行完毕!关闭数据库连接"

db2 terminate

 

  以上命令参考资料:http://blog.csdn.net/sweetbowie/article/details/19614289

 

  第三步:设置crond服务随Linux系统自启动

  root@localhost ~]# /etc/init.d/crond restart

  如果让crond在开机时运行,应该改变其运行级别

  [root@localhost ~]# chkconfig --levels 35 crond on

 

  【相关资料链接】

  1. http://blog.csdn.net/sweetbowie/article/details/19614289

  2. http://www.linuxidc.com/Linux/2012-02/54391.htm

  3. http://blog.csdn.net/liu_xing_hui/article/details/6565804/

  4. http://blog.csdn.net/lulei9876/article/details/9170151

  5. http://blog.csdn.net/jiujie395/article/details/8692655

  

Linux中定时执行DB2的存储过程

标签:

原文地址:http://www.cnblogs.com/maxiaolang/p/5439337.html

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