INSERT Query

  • Hello!

    Using ASP, with the RecordSet Object (objRS), I have a question

    When running the following query:

    "SELECT something FROM table"

    To get the value something, you would do:

    var = objRS("something").Value

    Now with the following scenario:

    Some tables have identy columns (that is set automatically when inserting row)

    Now I wonder, if I have the following table:

    [IDX] [int] IDENTITY (1, 1) NOT NULL,

    [something] [varchar] (32) NOT NULL

    If I would run the following query:

    "INSERT INTO table([something]) VALUES('sometext')"

    would I be able to do this:

    var = objRS("IDX").Value

    to get the value of the new IDX that was generated when creating this row in the database?

    Thanks,

  • Use this statement immediately after committing your insert to extract the last identity value used:

    SELECT @@IDENTITY FROM Table


    Regards,

    goodguy

    Experience is a bad teacher whose exams precede its lessons

  • Just a point to make...

    @@IDENTITY and SCOPE_IDENTITY return the last identity value generated in any table in the current session. However, SCOPE_IDENTITY returns the value only within the current scope; @@IDENTITY is not limited to a specific scope.

    --Ramesh


  • Thanks, Ramesh.


    Regards,

    goodguy

    Experience is a bad teacher whose exams precede its lessons

Viewing 4 posts - 1 through 3 (of 3 total)

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