create sequence TestIdentity
  start with 1
  increment by 1
  maxvalue 99999
  nocycle
  cache 20;create or replace trigger TestTrigger
  before insert on test for each row
    begin
      select TestIdentity.nextval into:new.ID from dual;
    end;我建了这样一个序列,把他应用到test表上,应该是从1开始,每次增加1,但是现在是从2开始,每次加2,这是为什么呢?

解决方案 »

  1.   

    SQL> create table test (id number,name varchar2(16));表已创建。SQL> create sequence TestIdentity
      2    start with 1
      3    increment by 1
      4    maxvalue 99999
      5    nocycle
      6    cache 20;序列已创建。SQL> create or replace trigger TestTrigger
      2    before insert on test for each row
      3      begin
      4        select TestIdentity.nextval into:new.ID from dual;
      5      end;
      6  /触发器已创建SQL> show error
    没有错误。
    SQL> insert into test (name) values ('AC米兰');已创建 1 行。SQL> commit;提交完成。SQL> select * from test;        ID NAME
    ---------- ----------------
             1 AC米兰SQL> insert into test (name) values ('AC米兰青年队');已创建 1 行。SQL> commit;提交完成。SQL> select * from test;        ID NAME
    ---------- ----------------
             1 AC米兰
             2 AC米兰青年队SQL> insert into test (name) values ('BRAZIL');已创建 1 行。SQL> commit;提交完成。SQL> select * from test;        ID NAME
    ---------- ----------------
             1 AC米兰
              2 AC米兰青年队
              3 BRAZILSQL>
      

  2.   

    你这个是触发器,那你表插入的时候也用了序列吧,
    你触发器里改成这个
    select TestIdentity.currval into:new.ID from dual; 
    试试,满足你的要求?>
      

  3.   

    现在的情况是,我表里如果没有DATE类型的字段,这个序列就能每次加1,但是我要是表里有date类型的字段,就每次加2,大家知道是为什么吗?
      

  4.   

    估计是在别的地方用了一次改序列,
    你可以试下,drop sequence,再重新创建,再引用nextval看看
      

  5.   

    你怎么插入数据的?
    insert into语句看看
      

  6.   

    insert into sysadmin.test values(TestIdentity.nextval,'abd',to_date('2009-06-23','YYYY-MM-DD'))
      

  7.   

    我只在test这个表上用序列了,别的表我没用,我也已经drop再重新创建了,就是不行,只要有日期函数就每次加2
      

  8.   

    SQL> create table test (id number,name varchar2(16),trans_date date);表已创建。SQL> create sequence TestIdentity
      2    start with 1
      3    increment by 1
      4    maxvalue 99999
      5    nocycle
      6    cache 20;序列已创建。SQL> create or replace trigger TestTrigger
      2    before insert on test for each row
      3      begin
      4        select TestIdentity.nextval into:new.ID from dual;
      5      end;
      6  /触发器已创建SQL> show error
    没有错误。
    SQL> insert into test (name) values ('AC米兰');已创建 1 行。SQL> select * from test;        ID NAME             TRANS_DATE
    ---------- ---------------- --------------
             1 AC米兰SQL> insert into test (name,trans_date) values ('AC米兰',sysdate);已创建 1 行。SQL> select * from test;        ID NAME             TRANS_DATE
    ---------- ---------------- --------------
             1 AC米兰
             2 AC米兰           05-3月 -10SQL> insert into sysadmin.test values(TestIdentity.nextval,'abd',to_date('2009-0
    6-23','YYYY-MM-DD'));
    insert into sysadmin.test values(TestIdentity.nextval,'abd',to_date('2009-06-23'
    ,'YYYY-MM-DD'))
                         *
    第 1 行出现错误:
    ORA-00942: 表或视图不存在
    SQL> insert into test values(TestIdentity.nextval,'abd',to_date('2009-06-23','YY
    YY-MM-DD'));已创建 1 行。SQL> select * from test;        ID NAME             TRANS_DATE
    ---------- ---------------- --------------
             1 AC米兰
             2 AC米兰           05-3月 -10
             4 abd              23-6月 -09SQL>
    你触发器已经select into用去一个值,但该值你并未使用,而是在insert into里,继续取下一个值。
      

  9.   

    测试了一下你的代码,没有问题。测试代码如下:
    create or replace trigger TestTrigger 
      before insert on T3 for each row 
        begin 
          select TEST_DUQ_SEQ.nextval into:new.A from dual; 
          DBMS_OUTPUT.put_line(:NEW.A);
        end; SQL>  INSERT INTO T3 VALUES(2,2);
    6--该值为触发器中打印出来的序列的值。已创建 1 行。向1楼说的那样,查看一下你的其他sql语句中是否调用了该sequence。
      

  10.   

    insert into sysadmin.test (col2,col3) values('abd',to_date('2009-06-23','YYYY-MM-DD'));commit;再看看
      

  11.   

    你的语句里用了TestIdentity.nextval
    触发器里取当前的TestIdentity.currval
      

  12.   


    TestIdentity.nextval又取了一次值。sequence又加了1.