Results 1 to 9 of 9

Thread: Oracle Autonumber?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140

    Oracle Autonumber?

    How do you create a field in an Oracle table so that it will generate its own key? I hope that makes sense.
    Travis, Kung Foo Journeyman
    As always, RTFM.

    WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
    Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    YBMS, but Mozilla doesn't.

  2. #2
    Addicted Member S@NSIS's Avatar
    Join Date
    Aug 2000
    Location
    Stoke-On-Trent, England
    Posts
    243
    From Deja...

    You can get a similar "autonumber" functionality programing an easy to code
    "before insert trigger" that gets the next value of the sequence and put it
    into the table for you.

    You must use a sequence. To create a sequence see the "Create Sequence"
    command of SQL Then on a trigger either on database (Before Insert) or
    on a form (PRE-INSERT) you write the following Code:

    Select Seq Name.NextVal
    Into Field Name
    From DUAL;

    where "Seq Name" the name of the sequence you created and "Field Name"
    the column which you want to be autonumbered.


    Let supose that we have a table named A with a primary key column named id.
    We also have a sequence called A_seq.

    The following code builds the trigger who generates the primary key
    automatically.

    create or replace trigger TBI_A before insert on A

    begin
    --this generates de
    select A_seq.nextval
    into :new.id
    from dual;
    end;
    Web/Application Developer
    VB6 Ent (SP5), Win 2000,SQL Server 2000

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140
    Okay... your response didn't help. and I'm not sure i understand the formatting.

    This is what I have....

    A table named "Errors". It has a field named "ErrorNum". In Access and SQL Server, any time a new record is added to that row, that field will generate its own value, a UID. I am trying to port this application to use an Oracle DB, but the DDLs I was given have not helped.

    I am trying to avoid changing the code to such a dramatic degree that I have two seperate products. How can I define this table to compensate for what the code is expecting?
    Travis, Kung Foo Journeyman
    As always, RTFM.

    WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
    Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    YBMS, but Mozilla doesn't.

  4. #4
    Addicted Member S@NSIS's Avatar
    Join Date
    Aug 2000
    Location
    Stoke-On-Trent, England
    Posts
    243
    Oracle doesn't have an autonumber as such.

    It has a 'Sequence' which can be used to figure out the next available UID before you insert a record.

    Therefore you really only have two options.

    1) Use the above. Get the next number in the sequence and use that.

    2) Create a GUID to use.

    Both of which will have to be put into a 'Before Insert' trigger.

    S@NSIS
    Web/Application Developer
    VB6 Ent (SP5), Win 2000,SQL Server 2000

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140
    Okay, I found this article: http://www.jlcomp.demon.co.uk/faq/autonumb.html.

    I think it is saying the same as you were (but with punctuation). If I understand, I can use this and it won't change the SQL statements already in the code, right?

    If so, this will work. We never supply this value, so I'm not worried about that, but we will query and return it to the user.
    Travis, Kung Foo Journeyman
    As always, RTFM.

    WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
    Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    YBMS, but Mozilla doesn't.

  6. #6
    Addicted Member S@NSIS's Avatar
    Join Date
    Aug 2000
    Location
    Stoke-On-Trent, England
    Posts
    243
    Your link doesn't work!

    As for 'punctuation', if you look at the top of the first post I made, I said it came from Deja (I actually searched for it).
    Web/Application Developer
    VB6 Ent (SP5), Win 2000,SQL Server 2000

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140
    *rotfl* Sorry, the forums incorporated the sentence ending period into the URL.

    It should be
    not
    http://www.jlcomp.demon.co.uk/faq/autonumb.html.
    Travis, Kung Foo Journeyman
    As always, RTFM.

    WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
    Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    YBMS, but Mozilla doesn't.

  8. #8
    Addicted Member S@NSIS's Avatar
    Join Date
    Aug 2000
    Location
    Stoke-On-Trent, England
    Posts
    243
    I see
    Web/Application Developer
    VB6 Ent (SP5), Win 2000,SQL Server 2000

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140
    Whahooo... it works.
    Travis, Kung Foo Journeyman
    As always, RTFM.

    WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
    Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    YBMS, but Mozilla doesn't.

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