This article mainly describes the actual operation process of the DB2 linked list storage process. In actual operations, we generate a linked list based on the T_depbal account balance table and transaction flow meter. In the zipper table, the start date is and the end date is 2050-12-31.
Create a table
- create table T_depchg
- (
- ACCOUNT_NO int,
- START_DATE DATE,
- ENT_DATE DATE,
- BALANCE double,
- AVG_BAL double
- )
- CREATE TABLE T_DEPBAL
- (
- ACCOUNT_NO int,
- BALANCE double
- )
------------------------------------------------------------------------------
- insert into t_journal values(date('2006-1-4'),2,200,1)
- create table t_journal
- (
- TRANS_DATE DATE,
- ACCOUNT_NO int,
- AMT double,
- FLAG double
- )
-
----------------------------------------------------------------
-DB2 linked list storage process
----------------------------------------------------------------
- create procedure bal
- RESULT SETS 1
- MODIFIES SQL DATA
- begin
- declare v_account int;
- declare v_balance int default 0;
- declare sqlcode int default 0;
- declare v_days int default 0;
- declare var_balance int default 0;
- declare v_all int default 0;
- declare v_acc int;
- declare v_start_date date;
- declare v_end_date date;
- declare v_trans_date date;
- declare v_max_date date;
- declare v_bt_days int default 0;
- declare v_in int;
- declare v_de int;
- declare v_amt int default 0;
- declare v_avg_bal int default 0;
- declare v_bal int;
- declare cur_account cursor with return for select * from t_depbal;
- --cur_journal definition
- declare cur_journal cursor for
- select account_no, trans_date,COALESCE(sum(Out) ,0) as Out,COALESCE(sum(de) ,0) as de,COALESCE(sum(de),0)-COALESCE(sum(Out) ,0) as re from
- (
- select trans_date,account_no,
- case when flag=0 then Amt end as Out,
- case when flag=1 then Amt end as de
- from t_journal where account_no =v_account
- Order By trans_Date
- ) t1
- group by trans_date, account_no
- order by account_no,trans_date;
- --max_date_cur
- declare max_date_cur cursor for
- select max(trans_date) from t_journal
- where account_no=v_account;
- open cur_account
- delete from t_depchg;
- open cur_account;
- out_loop:
- loop
- fetch cur_account into v_account,v_balance;
- IF SQLCODE =100 then leave out_loop;
- end if;
- INSERT INTO DEMO VALUES(V_ACCOUNT,V_BALANCE);
- set var_balance=v_balance;
- set v_all=0;
- set v_start_date =date('2006-1-1');
- -- open cur_journal
- open cur_journal;
- fetch_loop:
- loop
- fetch cur_journal into v_acc,v_trans_date,v_in,v_de,v_amt;
- IF SQLCODE =100 then leave fetch_loop;
- end if;
- set v_bal = var_balance;
-
- set v_end_date = v_trans_date;
- set v_days=days(v_end_date)-days(date('2006-1-1'));
- set v_bt_days=(days(v_end_date)-days(v_start_date));
- set v_allv_all =v_all+(var_balance*v_bt_days);
- set v_avg_bal = v_all/v_days;
- insert into t_depchg values(v_acc,v_start_date,v_end_date,v_bal,v_avg_bal);
- set var_balancevar_balance=var_balance+v_amt;
- set v_start_date =v_end_date;
- -- processing dead date
- if v_start_date=v_max_date then
- set v_days=days('2050-12-31')-days(date('2006-1-1'));
- set v_bt_days=(days('2050-12-31')-days(v_start_date));
- set v_allv_all =v_all+(var_balance*v_bt_days);
- set v_avg_bal = v_all/v_days;
- insert into t_depchg values(v_acc,v_start_date,date('2050-12-31'),var_balance,v_avg_bal);
- end if;
- open max_date_cur;
- date_loop:
- loop
- fetch max_date_cur into v_max_date;
- IF SQLCODE =100 then leave date_loop;
- end if;
- end loop;
- close max_date_cur;
- end loop;
- close cur_journal;
- end loop;
- close cur_account;
- end@
- drop procedure bal
- call bal
- select * from t_depchg
The above content is an introduction to the DB2 linked list storage process. I hope you will find some gains.