atuomatically generated value in database?

  • how can we insert value of one attribute in the database automatically??i.e. it should be automatically generate. i'm using sql server 2000.

  • Care to elaborate a bit?

    Is this about using the identity datatype or using a default value?

  • Agreed... a little more info would help... should it be a date, and sequential ID, what???

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Automatical generated values are in common defaults which a bound to a column. An example for a special default is the identity column which increments itself for each insert made. If you want to have another / a custom incrementation you will have to code that on your own in a function. The function can be itself bound to a default constraint and this to a table.

    HTH, Jens Suessmeyer.

    ---

    http://www.sqlserver2005.de

    ---

  • yes; it's a sequential id

  • Great... and simple, too...

    When you create a table, create your "sequential ID" column with the IDENTITY property...

    For example... this will create a table, insert 3 rows, insert 3 more rows, then list all the rows by the sequential ID and then list all the rows by the other column... notice how the RowID column works... you can find out a lot more looking up CREATE TABLE and IDENTITY in Books Online...

     CREATE TABLE JustaTest

            (

            RowNum       INTEGER IDENTITY(1,1) PRIMARY KEY CLUSTERED,

            SomeOtherCol VARCHAR(10)

            )

     INSERT INTO JustaTest

            (SomeOtherCol)

     SELECT 'Sam' UNION ALL

     SELECT 'Dean' UNION ALL

     SELECT 'Sally'

     INSERT INTO JustaTest

            (SomeOtherCol)

     SELECT 'Joe' UNION ALL

     SELECT 'Andy' UNION ALL

     SELECT 'Adelle'

     SELECT *

       FROM JustaTest

      ORDER BY RowNum

     SELECT *

       FROM JustaTest

      ORDER BY SomeOtherCol

     

     

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply