如何在plsql的begin和end之间使用type is record、is table of声明的collect集合呢?

青旅半醒 2023-08-17 15:20 89阅读 0赞

declare里进行声明一个类型type is record,这里必须写多列,否则在bulk collect里就不能使用is table of 的类型的对象了。

举例:

  1. declare
  2. type type_number is record (
  3. center_name varchar2(128),
  4. amounts number(20, 3)
  5. );
  6. type type_list_amount is table of type_number;
  7. v_list type_list_amount;
  8. v_amount_sum number(20,3);
  9. begin
  10. select distinct podt.center_code||':'||accc.center_name,sum(abs(ppjl.AMOUNT)) bulk collect
  11. into v_list
  12. from podt,
  13. codt,
  14. accc,
  15. PPJL
  16. where codt.policy_no = podt.policy_no
  17. and podt.center_code = accc.center_code
  18. and ppjl.POLICY_NO = podt.POLICY_NO
  19. and codt.coverage_no = '1'
  20. and (podt.po_status_code in ('A', 'L', 'M', 'P', 'R', 'U', 'W', 'Z') or
  21. (podt.po_status_code in ('T', 'S', 'D', 'J', 'Y', 'Z')))
  22. and (codt.co_status_code in ('P', 'R', 'A', 'L', 'M', 'U', 'W') or
  23. (codt.co_status_code in ('T', 'E', 'S', 'D', 'J', 'Y', 'Z')))
  24. and exists(select * from ppjl where ACCOUNT_CODE like '4101%')
  25. and ppjl.TRAN_DATE >= to_date('2018/01/01', 'YYYY/MM/DD')
  26. and ppjl.TRAN_DATE <= to_date('2018/12/31', 'YYYY/MM/DD')
  27. group by podt.center_code, accc.center_name;
  28. v_amount_sum := 0;
  29. /**
  30. 所有机构的有效保单号的累计缴额
  31. */
  32. for i in 1.. v_list.COUNT loop
  33. v_amount_sum := v_amount_sum + v_list(i).amounts;
  34. end loop;
  35. DBMS_OUTPUT.put_line('保单保费总算:' || v_amount_sum);
  36. end;

但是可以这样用:

  1. declare
  2. type type_varchar2s is table of varchar2(20);
  3. v_list type_varchar2s;
  4. begin
  5. select POLICY_NO bulk collect
  6. into v_list
  7. from PODT;
  8. for i in 1..v_list.COUNT loop
  9. DBMS_OUTPUT.put_line(v_list(i));
  10. end loop;
  11. end;

发表评论

表情:
评论列表 (有 0 条评论,89人围观)

还没有评论,来说两句吧...

相关阅读