关于重新打开数据库后,package重新执行的问题
我请人做的一个package的功能是网数据库中插入数据时,第一条记录插入p_date字段的内容(用的是pk_myprocess.get_p_date)是一个初始时间的10点,第二条记录是这个时间的16点,第三条是下一天的10点,第四条是下一天的16点,第五条是第3天的10点,第六条是第3天的16点,依次类推,我请人给写的如果是在一次打开数据库时能正常执行,但是如果关闭后,重新打开就又从第一天的10点开始执行,请大狭们帮改改,如何能让他再次打开数据后,pk_myprocess.get_p_date的值仍得到下一个值,而不是重新开始.
代码如下.
SQL> drop package pk_myprocess;
Package dropped
SQL> drop package body pk_myprocess;
drop package body pk_myprocess
ORA-04043: object PK_MYPROCESS does not exist
SQL> create or replace package pk_myprocess
2 is
3 function get_p_date return date;
4 function rollback_date return date;
5 end pk_myprocess;
6 /
Package created
SQL> create or replace package body pk_myprocess
2 is
3 last_date date;
4 function get_p_date return date
5 is
6 v_pdate date;
7 begin
8 last_date := nvl(last_date,trunc(sysdate));
9 if last_date = to_date(to_char(last_date,'yyyy-mm-dd')||' 10:00:00','yyyy-mm-dd hh24:mi:ss') then
10 v_pdate := to_date(to_char(last_date,'yyyy-mm-dd')||' 16:00:00','yyyy-mm-dd hh24:mi:ss');
11 else
12 if last_date < to_date(to_char(last_date,'yyyy-mm-dd')||' 10:00:00','yyyy-mm-dd hh24:mi:ss') then
13 v_pdate := to_date(to_char(last_date ,'yyyy-mm-dd')||' 10:00:00','yyyy-mm-dd hh24:mi:ss');
14 else
15 v_pdate := to_date(to_char(last_date + 1,'yyyy-mm-dd')||' 10:00:00','yyyy-mm-dd hh24:mi:ss');
16 end if;
17 end if;
18 last_date := v_pdate;
19 return v_pdate;
20 end get_p_date;
21 function rollback_date return date
22 is
23 v_pdate date;
24 begin
25 last_date := nvl(last_date,sysdate - 1);
26 if last_date = to_date(to_char(last_date,'yyyy-mm-dd')||' 16:00:00','yyyy-mm-dd hh24:mi:ss') then
27 v_pdate := to_date(to_char(last_date,'yyyy-mm-dd')||' 10:00:00','yyyy-mm-dd hh24:mi:ss');
28 else
29 v_pdate := to_date(to_char(last_date - 1,'yyyy-mm-dd')||' 10:00:00','yyyy-mm-dd hh24:mi:ss');
30 end if;
31 last_date := v_pdate;
32 return v_pdate;
33 end rollback_date;
34
35 end pk_myprocess;
36 /
Package body created
SQL> drop table test_a;
Table dropped
SQL> create table test_a(id number,edit_date date,p_date date);
Table created
SQL> insert into test_a values(1,sysdate,pk_myprocess.get_p_date);
1 row inserted
SQL> commit;
Commit complete
SQL> insert into test_a values(2,sysdate,pk_myprocess.get_p_date);
1 row inserted
SQL> insert into test_a values(3,sysdate,pk_myprocess.get_p_date);
1 row inserted
SQL> select id,to_char(edit_date,'yyyy-mm-dd hh24:mi:ss') as edit_date,
2 to_char(p_date,'yyyy-mm-dd hh24:mi:ss') as p_date from test_a;
ID EDIT_DATE P_DATE
---------- ------------------- -------------------
1 2007-04-27 17:31:19 2007-04-27 10:00:00
2 2007-04-27 17:31:20 2007-04-27 16:00:00
3 2007-04-27 17:31:20 2007-04-28 10:00:00
SQL>