代码之家  ›  专栏  ›  技术社区  ›  LPK

如何在插入新行时自动增加列中的值

  •  1
  • LPK  · 技术社区  · 7 年前

     COMPUTED column and LAST_VALUE(column) + 1
    

    所以我有以下要求:

    ALTER TABLE schema.table (
    
    ADD SK NUMBER ALWAYS AS (LAST_VALUE(SK)+1)
    
    );
    

    还是需要添加 FOR EACH ROW

    顺序:

    create sequence SK_SEQUENCES
    increment by 1
    start with 1
      nomaxvalue
      minvalue 1
    nocycle
    order
    keep;
    

    create table schema.test(
    isCurrent CHAR(10),
    SK NUMBER
    );     
    

    SK

    所以我有这个:

    insert into schema.test(SK)
    values (SK_SEQUENCES.nextval)
    

    isCurrent

    磁盘

    1 回复  |  直到 7 年前
        1
  •  2
  •   William Robertson    7 年前

    首先,语法是 generated always always add

    alter table demo
    add sk integer generated always as (last_value(sk)+1);
    

    失败原因:

    ORA-30484: missing window specification for this function
    

    last_value over (partition by xxx order by yyy) .不能将分析函数用作列默认值。

    alter table demo
    add sk integer generated always as identity;
    

    create sequence sk_seq;
    
    create or replace trigger demo_generate_sk_trg
    before insert on demo for each row
    begin
        :new.dummy := sk_seq.nextval;
    end;
    /