Description of the implementation process of the DB2 linked list Stored Procedure

Source: Internet
Author: User

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

 
 
  1. create table T_depchg  
  2. (  
  3. ACCOUNT_NO int,  
  4. START_DATE DATE,  
  5. ENT_DATE DATE,  
  6. BALANCE double,  
  7. AVG_BAL double  
  8. )  
  9. CREATE TABLE T_DEPBAL  
  10. (  
  11. ACCOUNT_NO int,  
  12. BALANCE double  
  13. )  

------------------------------------------------------------------------------

 
 
  1. insert into t_journal values(date('2006-1-4'),2,200,1)  
  2. create table t_journal  
  3. (  
  4. TRANS_DATE DATE,  
  5. ACCOUNT_NO int,  
  6. AMT double,  
  7. FLAG double  
  8. )  
  9.  

----------------------------------------------------------------
-DB2 linked list storage process
----------------------------------------------------------------

 
 
  1. create procedure bal  
  2. RESULT SETS 1  
  3. MODIFIES SQL DATA  
  4. begin  
  5. declare v_account int;  
  6. declare v_balance int default 0;  
  7. declare sqlcode int default 0;  
  8. declare v_days int default 0;  
  9. declare var_balance int default 0;  
  10. declare v_all int default 0;  
  11. declare v_acc int;  
  12. declare v_start_date date;  
  13. declare v_end_date date;  
  14. declare v_trans_date date;  
  15. declare v_max_date date;  
  16. declare v_bt_days int default 0;  
  17. declare v_in int;  
  18. declare v_de int;  
  19. declare v_amt int default 0;  
  20. declare v_avg_bal int default 0;  
  21. declare v_bal int;  
  22. declare cur_account cursor with return for select * from t_depbal;  
  23. --cur_journal definition  
  24. declare cur_journal cursor for   
  25. 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   
  26. (  
  27. select trans_date,account_no,  
  28. case when flag=0 then Amt end as Out,  
  29. case when flag=1 then Amt end as de  
  30. from t_journal where account_no =v_account 
  31. Order By trans_Date   
  32. ) t1  
  33. group by trans_date, account_no  
  34. order by account_no,trans_date;  
  35. --max_date_cur   
  36. declare max_date_cur cursor for   
  37. select max(trans_date) from t_journal  
  38. where account_no=v_account;  
  39. open cur_account  
  40. delete from t_depchg;  
  41. open cur_account;  
  42. out_loop:  
  43. loop  
  44. fetch cur_account into v_account,v_balance;  
  45. IF SQLCODE =100 then leave out_loop;  
  46. end if;  
  47. INSERT INTO DEMO VALUES(V_ACCOUNT,V_BALANCE);  
  48. set var_balance=v_balance;  
  49. set v_all=0;  
  50. set v_start_date =date('2006-1-1');  
  51. -- open cur_journal  
  52. open cur_journal;  
  53. fetch_loop:  
  54. loop  
  55. fetch cur_journal into v_acc,v_trans_date,v_in,v_de,v_amt;  
  56. IF SQLCODE =100 then leave fetch_loop;  
  57. end if;  
  58. set v_bal = var_balance;  
  59.  
  60. set v_end_date = v_trans_date;  
  61. set v_days=days(v_end_date)-days(date('2006-1-1'));  
  62. set v_bt_days=(days(v_end_date)-days(v_start_date));  
  63. set v_allv_all =v_all+(var_balance*v_bt_days);  
  64. set v_avg_bal = v_all/v_days;  
  65. insert into t_depchg values(v_acc,v_start_date,v_end_date,v_bal,v_avg_bal);   
  66. set var_balancevar_balance=var_balance+v_amt;  
  67. set v_start_date =v_end_date;  
  68. -- processing dead date  
  69. if v_start_date=v_max_date then  
  70. set v_days=days('2050-12-31')-days(date('2006-1-1'));  
  71. set v_bt_days=(days('2050-12-31')-days(v_start_date));  
  72. set v_allv_all =v_all+(var_balance*v_bt_days);  
  73. set v_avg_bal = v_all/v_days;  
  74. insert into t_depchg values(v_acc,v_start_date,date('2050-12-31'),var_balance,v_avg_bal);   
  75. end if;  
  76. open max_date_cur;  
  77. date_loop:  
  78. loop  
  79. fetch max_date_cur into v_max_date;  
  80. IF SQLCODE =100 then leave date_loop;  
  81. end if;  
  82. end loop;  
  83. close max_date_cur;  
  84. end loop;  
  85. close cur_journal;  
  86. end loop;  
  87. close cur_account;  
  88. end@  
  89. drop procedure bal  
  90. call bal  
  91. select * from t_depchg  

The above content is an introduction to the DB2 linked list storage process. I hope you will find some gains.


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.