看到有人问类似的问题,所以做了一个简单的设计,本身未接触过类似的领域,当作乐趣测试了一把,勿与实际设计的系统挂钩。
测试过程如下:
drop table test1;
drop table test2;
drop table test3;
--帐套批次所允许使用的发票区间段
create table test1(
batch_id number,
min_no number,
max_no number
) tablespace test_data;
--帐套批次已使用的发票号区间段
create table test2(
batch_id number,
begin_no number,
end_no number
) tablespace test_data;
--帐套批次未使用的发票号区间段
create table test3(
batch_id number,
begin_no number,
end_no number
) tablespace test_data;
insert into test1(batch_id,min_no,max_no) values(1,1,100);
insert into test2(batch_id,begin_no,end_no) values(1,1,1);
insert into test2(batch_id,begin_no,end_no) values(1,20,40);
insert into test2(batch_id,begin_no,end_no) values(1,41,50);
insert into test2(batch_id,begin_no,end_no) values(1,70,99);
commit;
CREATE OR REPLACE PROCEDURE proc_test_billno (p_batch_id NUMBER)
AS
v_current_bno NUMBER;
v_current_eno NUMBER;
v_last_bno NUMBER;
v_last_eno NUMBER;
v_min_no NUMBER;
v_max_no NUMBER;
CURSOR c
IS
SELECT begin_no, end_no
FROM test2
WHERE batch_id = p_batch_id
ORDER BY begin_no;
BEGIN
SELECT min_no, max_no
INTO v_min_no, v_max_no
FROM test1
WHERE batch_id = p_batch_id;
v_last_bno := v_min_no;
v_last_eno := v_max_no;
FOR cc IN c LOOP
IF v_last_bno < cc.begin_no THEN
v_current_bno := v_last_bno;
v_current_eno := cc.begin_no - 1;
INSERT INTO test3(batch_id, begin_no, end_no)
VALUES (p_batch_id, v_current_bno, v_current_eno);
v_last_bno := cc.end_no + 1;
dbms_output.put_line('if: ' || v_last_bno);
ELSIF v_last_bno = cc.begin_no THEN
v_last_bno := cc.end_no + 1;
dbms_output.put_line('else if: ' || v_last_bno);
END IF;
END LOOP;
IF v_last_bno <= v_max_no THEN
v_current_bno := v_last_bno;
v_current_eno := v_max_no;
INSERT INTO test3(batch_id, begin_no, end_no)
VALUES (p_batch_id, v_current_bno, v_current_eno);
END IF;
COMMIT;
END;
/
SQL> select * from test1;
BATCH_ID MIN_NO MAX_NO
---------- ---------- ----------
1 1 100
SQL> select * from test2;
BATCH_ID BEGIN_NO END_NO
---------- ---------- ----------
1 1 1
1 20 40
1 41 50
1 70 99
SQL> exec proc_test_billno(1);
PL/SQL procedure successfully completed.
SQL> select * from test3;
BATCH_ID BEGIN_NO END_NO
---------- ---------- ----------
1 2 19
1 51 69
1 100 100
Posted by 枯荣长老
@ 01:14 下午 CST
[
Comments [0]
]