系统里有张表有一个 报表信息编号 字段 为主键,其类型为VARCHAR2(20),如何设计使其自动增量,并且编号生成规则为:日期+当日编号    例:200812040001。

解决方案 »

  1.   

    create sequence seq1;
    create or replace trigger t
      before insert on test
      for each rowbegin
      select to_char(sysdate, 'yyyymmdd') || lpad(seq1.nextval, 5, 0)
        into :new.id
        from dual;
    end;SQL> insert into test(name) values('lisi');
     
    1 row inserted
     
    SQL> insert into test(name) values('zhangsan');
     
    1 row inserted
     
    SQL> select * from test;
     
    ID                   NAME
    -------------------- --------------------
    2008121100001        lisi
    2008121100002        zhangsan
      

  2.   

    建立一个SEQUENCE和一个触发器 
    触发器的语句为 create or replace tr_test before insert on yourtable for each row
    begin
      select to_char(sysdate,'yyyymmdd')||yourseq.nextval into :new.报表信息编号 from dual;
    end;