DB2存储过程一些技巧 博客分类: 数据库 DB2
程序员文章站
2024-03-23 20:00:58
...
1 循环
1.1 while
示例:
SET vCallTime = DATE('2011-05-01');
WHILE (vCallTime <= Date('2011-06-10')) do
-- PLSQL
SET vCallTime = vCallTime + 1 days;
END WHILE;
1.2 Loop
示例:
DECLARE AT_END INTEGER DEFAULT 1;
DECLARE CONTINUE HANDLER FOR NOT FOUND
BEGIN
SET AT_END=0;
END;
OPEN curInsuranceKind;
kindLoop:
LOOP
FETCH curInsuranceKind INTO vInsuranceClassName;
IF AT_END=0 THEN
LEAVE kindLoop;
END IF;
-- PLSQL
END LOOP;
2 时间
2.1 某月有多少天
例如,2011年2月份有多少天
SELECT DAYS('2011-03-01') - DAYS('2011-02-01') FROM sysibm.sysdummy1
2.2 今天是今年的第几天
SELECT DAYS('今天日期') - DAYS('2011-01-01') + 1 FROM sysibm.sysdummy1
3 获得执行命令影响的行数
示例:
DECLARE SEQ_COL INTEGER;
SELECT COUNT(1) INTO SEQ_COL
FROM FINAL TABLE(
UPDATE DB2ADMIN.DW_BIZ2006
SET PAIDNO_SQ = vPaidno,
PAIDLOSS_SQM = vPaidloss,
SUMOSNO_SQM = vSumossno,
SUMOSLOSS_SQM = vSumosloss,
POLICYCOUNT_SQM = vPolicycount,
PREMIUM_SQM = vPremium
WHERE DESKDATE = vCollectNextMonthYear AND
COMCODE = vComCode AND
KINDCODE = vKindCode);
注意
做除法时,除数和被除数都不能为零,要做判断
1.1 while
示例:
SET vCallTime = DATE('2011-05-01');
WHILE (vCallTime <= Date('2011-06-10')) do
-- PLSQL
SET vCallTime = vCallTime + 1 days;
END WHILE;
1.2 Loop
示例:
DECLARE AT_END INTEGER DEFAULT 1;
DECLARE CONTINUE HANDLER FOR NOT FOUND
BEGIN
SET AT_END=0;
END;
OPEN curInsuranceKind;
kindLoop:
LOOP
FETCH curInsuranceKind INTO vInsuranceClassName;
IF AT_END=0 THEN
LEAVE kindLoop;
END IF;
-- PLSQL
END LOOP;
2 时间
2.1 某月有多少天
例如,2011年2月份有多少天
SELECT DAYS('2011-03-01') - DAYS('2011-02-01') FROM sysibm.sysdummy1
2.2 今天是今年的第几天
SELECT DAYS('今天日期') - DAYS('2011-01-01') + 1 FROM sysibm.sysdummy1
3 获得执行命令影响的行数
示例:
DECLARE SEQ_COL INTEGER;
SELECT COUNT(1) INTO SEQ_COL
FROM FINAL TABLE(
UPDATE DB2ADMIN.DW_BIZ2006
SET PAIDNO_SQ = vPaidno,
PAIDLOSS_SQM = vPaidloss,
SUMOSNO_SQM = vSumossno,
SUMOSLOSS_SQM = vSumosloss,
POLICYCOUNT_SQM = vPolicycount,
PREMIUM_SQM = vPremium
WHERE DESKDATE = vCollectNextMonthYear AND
COMCODE = vComCode AND
KINDCODE = vKindCode);
注意
做除法时,除数和被除数都不能为零,要做判断
推荐阅读
-
db2常用命令 博客分类: db2 DB2数据库数据命令
-
DB2存储过程一些技巧 博客分类: 数据库 DB2
-
DB2 大数据导入注意 与 客户端连接数据库用名 博客分类: sql DB2
-
存储过程(sp)的应用 博客分类: 数据库 Oracle
-
存储过程(sp)的应用 博客分类: 数据库 Oracle
-
数据库索引创建规则 博客分类: orale,db2,mysql等各种数据库创建索引 OracleSQL ServerSybase
-
DB2错误代码大全 博客分类: 数据库 db2
-
db2的常用操作 博客分类: db2数据库 db2
-
DB2 自增长列导入、导出测试 博客分类: 数据库 db2
-
db2将非 Unicode 数据库转换为 Unicode 数据库 博客分类: db2 db2Unicodedb2movedb2look