plsql插入,并按规则生成单号

2018-07-03 15:23 更新
  1. <insert id="insertSelective" parameterType="com.tcl.srm.vendor.vo.TQiHeader" >
  2. <selectKey keyProperty="qiCode" resultType="java.lang.String" order="BEFORE">
  3. select 'QI' || to_char(sysdate,'yyyymmdd') || lpad(seq_qi_id.nextval,4,'0') as QI_CODE from dual
  4. </selectKey>
  5. insert into T_QI_HEADER
  6. <trim prefix="(" suffix=")" suffixOverrides="," >
  7. <if test="qiCode != null" >
  8. QI_CODE,
  9. </if>
  10. <if test="companyCode != null" >
  11. COMPANY_CODE,
  12. </if>
  13. <if test="sourceType != null" >
  14. SOURCE_TYPE,
  15. </if>
  16. <if test="sourceCode != null" >
  17. SOURCE_CODE,
  18. </if>
  19. <if test="vendorCode != null" >
  20. VENDOR_CODE,
  21. </if>
  22. <if test="vendorName != null" >
  23. VENDOR_NAME,
  24. </if>
  25. <if test="vendorTel != null" >
  26. VENDOR_TEL,
  27. </if>
  28. </trim>
  29. <trim prefix="values (" suffix=")" suffixOverrides="," >
  30. <if test="qiCode != null" >
  31. #{qiCode,jdbcType=VARCHAR},
  32. </if>
  33. <if test="companyCode != null" >
  34. #{companyCode,jdbcType=VARCHAR},
  35. </if>
  36. <if test="sourceType != null" >
  37. #{sourceType,jdbcType=VARCHAR},
  38. </if>
  39. <if test="sourceCode != null" >
  40. #{sourceCode,jdbcType=VARCHAR},
  41. </if>
  42. <if test="vendorCode != null" >
  43. #{vendorCode,jdbcType=VARCHAR},
  44. </if>
  45. <if test="vendorName != null" >
  46. #{vendorName,jdbcType=VARCHAR},
  47. </if>
  48. <if test="vendorTel != null" >
  49. #{vendorTel,jdbcType=VARCHAR},
  50. </if>
  51. </trim>
  52. </insert>

其中:

<selectKey keyProperty="qiCode" resultType="java.lang.String" order="BEFORE"> select 'QI' || to_char(sysdate,'yyyymmdd') || lpad(seq_qi_id.nextval,4,'0') as QI_CODE from dual </selectKey>` 即为主键生成规则; 先创建一个序列seq_qi_id 写一个存储过程用于每天初始化序列从0开始:

create or replace procedure CLEARSEQ_QI as n_count number(8); begin select count(1) into n_count from user_sequences t where t.sequence_name = 'SEQ_QI_ID';

  1. if n_count > 0 then
  2. execute immediate 'drop sequence SEQ_QI_ID';
  3. end if;
  4. execute immediate 'create sequence SEQ_QI_ID
  5. minvalue 1
  6. maxvalue 99999999
  7. start with 1
  8. increment by 1
  9. NOCYCLE
  10. NOCACHE';
  11. commit;
  12. end;

每天需要对该存储过程计数清零:

create or replace package body PG_CLEAR_ALL_SEQ is

  1. PROCEDURE mainproc aS
  2. procedure_name varchar2(40) := 'PG_CLEAR_ALL_SEQ.mainproc';
  3. BEGIN
  4. rlog.info(procedure_name, 0, '', 'Start,iv_id is PG_CLEAR_ALL_SEQ' );
  5. COMMIT;
  6. clearseq_qi();
  7. COMMIT;
  8. rlog.info(procedure_name, 0, '', 'Finished,iv_id is PG_CLEAR_ALL_SEQ');
  9. COMMIT;
  10. EXCEPTION
  11. WHEN OTHERS THEN
  12. rlog.except(procedure_name,
  13. '1',
  14. SUBSTR(SQLERRM, 1, 512),
  15. SUBSTR(DBMS_UTILITY.format_error_backtrace, 1, 512));
  16. COMMIT;
  17. END;
  18. end PG_CLEAR_ALL_SEQ;

(附加)根据主表单号生成对应下一级明细条目号:

<selectKey keyProperty="modelItemCode" resultType="java.lang.String" order="BEFORE"> select #{modelCode} || '_' || lpad((decode(max(to_number(substr (m.MODEL_ITEM_CODE,18))), null, 0, max(to_number(substr(m.MODEL_ITEM_CODE,18)))) + 1),2,'0') from T_VD_INDICATORS_MODEL_DETAIL m where m.MODEL_CODE = #{modelCode} </selectKey>

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号