DBAsupport.com Forums - Powered by vBulletin
Results 1 to 7 of 7

Thread: auto incrementing a field in a table

  1. #1
    Join Date
    Jul 2000
    Posts
    70

    Exclamation

    How can I set a number field in a table to be auto-increment.
    Basically the same concept that is used in MS Access, where the field value is automatically incremented when a new row is inserted.
    thanks in advance.

  2. #2
    Join Date
    Nov 2000
    Location
    greenwich.ct.us
    Posts
    9,092
    You will have to write a before-insert trigger that fetches a value from a sequence before the row gets inserted into the database.
    Jeff Hunter

  3. #3
    Join Date
    Jul 2000
    Posts
    70
    could you provide me with a sample code that do this?

  4. #4
    Join Date
    Dec 2000
    Location
    Brazil / São Paulo
    Posts
    97
    Use SEQUENCE.

    Example :

    CREATE SEQUENCE TEST
    INCREMENT BY 1
    START WITH 1
    /


    Insert Into <table>
    (sequence, name, address)
    Values
    (TEST.NEXT_VALUE,
    'Marcio Almeida',
    'R Cupa, 139');

    Márcio de Souza Almeida
    DBA Oracle / SQLServer / PostgreSQL
    Rua Cupa, 139 Apto 85 A
    Penha - São Paulo - SP
    03640-000 - Brasil
    http://www.directory.com.br

  5. #5
    Join Date
    Jul 2000
    Posts
    70
    what is the type of sequence in the example?
    i keep getting a ORA-00984: column not allowed here.

  6. #6
    Join Date
    Nov 2000
    Location
    New Delhi, India
    Posts
    81

    Red face

    The type of column "sequence" should be numeric.
    If your actual table.column is a varchar then use
    to_char(TEST.NEXT_VALUE) in the insert statement.

  7. #7
    Join Date
    Dec 2000
    Posts
    28
    The error "ORA-00984: column not allowed here" is coming because you are refering the sequence as next value as NEXT_VALUE. This is wrong. It should be NEXTVAL. You change this and it will work fine.

    thanks,
    ad

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Click Here to Expand Forum to Full Width