我用JSP+ORACLE做的一个系统,其中有项功能是在网数据库添加内容时的XTIME字段,网里放的时候第一条纪录初始一个时间(如2007年04月20日10点),而第二条纪录放进去是2007年04月20日16点,第三条纪录放进去是2007年04月21日10点,第四条纪录是2007年04月21日16点.......以此类推,按天循环,就是每天2次,哪位高手能帮我改一下或者是写个时间序列.
大致代码如下:
String NewVals,InsertSQL,Columns;
  int prov=4;String XTIME="2007-04-20-10"
  Columns="content,prov,sendtime,xuhao,fenlei,XTIME";
  NewVals="'"+address+"','"+prov+"',sysdate,ny.nextval,'"+StartTime+"',
'"+XTIME+"";
  InsertSQL="INSERT INTO SMS_LH_CONTENT("+Columns+") VALUES ("+IsoConvertGb(NewVals)+")";

解决方案 »

  1.   

    逻辑不完整,如果当天插入了两条以上的记录怎么办?
    不考虑异常,可以这么做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 createdSQL> 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      if last_date = to_date(to_char(sysdate,'yyyy-mm-dd')||' 10:00:00','yyyy-mm-dd hh24:mi:ss') then
      9         v_pdate := to_date(to_char(sysdate,'yyyy-mm-dd')||' 16:00:00','yyyy-mm-dd hh24:mi:ss');
     10      else
     11         v_pdate := to_date(to_char(sysdate,'yyyy-mm-dd')||' 10:00:00','yyyy-mm-dd hh24:mi:ss');
     12         last_date := v_pdate;
     13      end if;
     14      return v_pdate;
     15    end get_p_date;
     16    function rollback_date return date
     17    is
     18      v_pdate  date;
     19    begin
     20      last_date := nvl(last_date,sysdate - 1);
     21      if last_date = to_date(to_char(last_date,'yyyy-mm-dd')||' 16:00:00','yyyy-mm-dd hh24:mi:ss') then
     22         v_pdate := to_date(to_char(last_date,'yyyy-mm-dd')||' 10:00:00','yyyy-mm-dd hh24:mi:ss');
     23      else
     24         v_pdate := to_date(to_char(last_date - 1,'yyyy-mm-dd')||' 10:00:00','yyyy-mm-dd hh24:mi:ss');
     25         last_date := v_pdate;
     26      end if;
     27      return v_pdate;
     28    end rollback_date;
     29  
     30  end pk_myprocess;
     31  /Package body createdSQL> create table test_a(id number,edit_date date,p_date date);SQL> insert into test_a values(1,sysdate,pk_myprocess.get_p_date);1 row insertedSQL> commit;Commit completeSQL> insert into test_a values(2,sysdate,pk_myprocess.get_p_date);1 row insertedSQL> commit;Commit complete
    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-26 11:14:16 2007-04-26 10:00:00
             2 2007-04-26 11:14:16 2007-04-26 16:00:00
      

  2.   

    楼上的ORACLE水平确实不错,这100分现在至少80分是你的了。感谢你的热心帮助。
      

  3.   

    你的QQ是多少了  我的QQ是121716761 看到后加我
      

  4.   

    我加你QQ了,我是对ORACLE的存储过程一点也不懂呀,你不忙时候帮我改改.
      

  5.   

    SQL> drop package pk_myprocess;Package droppedSQL> drop package body pk_myprocess;drop package body pk_myprocessORA-04043: object PK_MYPROCESS does not existSQL> 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 createdSQL> 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 createdSQL> drop table test_a;Table droppedSQL> create table test_a(id number,edit_date date,p_date date);Table createdSQL> insert into test_a values(1,sysdate,pk_myprocess.get_p_date);1 row insertedSQL> commit;Commit completeSQL> insert into test_a values(2,sysdate,pk_myprocess.get_p_date);1 row insertedSQL> insert into test_a values(3,sysdate,pk_myprocess.get_p_date);1 row insertedSQL> 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:00SQL>
      

  6.   

    adaizi1980
    我用这个到网页就不知道为什么不对了 在数据库就可以
    我在网业里都是显示4/27/2007 10:0:0  不管多少条 以后都是这个时间
    你懂JSP吗 这是我在网业中用的插入语句  XTIME就是调用的这个函数
    你帮我看看是哪错了
     Columns="content,prov,sendtime,xuhao,fenlei,xtime";
      NewVals="'"+address+"','"+prov+"',sysdate,ny.nextval,'"+StartTime+"',pk_myprocess.get_p_date";
      InsertSQL="INSERT INTO SMS_LH_CONTENT("+Columns+") VALUES ("+IsoConvertGb(NewVals)+")";
      

  7.   

    我知道了,可能是因为关了一次数据库后又重新执行,比如开始pk_myprocess.get_p_date是4/27/2007 10:0:0 用了5次后成了4/30/2007 16:0:0,但是如果此时关了数据库就第一次用又成了4/27/2007 10:0:0 网业上就是添加一次记录后关闭了又重添加,这种该如何解决呢?大狭再帮我看看该如何解决