Last insert id

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • George Hill

    Last insert id

    Hi

    I am trying to import several master detail records from files to ms sql
    server.
    I have orders file and order_items file that has several rows for each
    order.
    If I insert programmaticall y these records how can find out which order ID
    was the last inserted, so that I can attach the subsesquent row items to a
    proper order.

    I am quite new to ms sql server. I have used mysql a lot and there I could
    use mysql_insert_id to find out the last autoincremented filed number.
    I am looking for a similar method for ms sql server 2000.

    TIA
    George


  • Simon Hayes

    #2
    Re: Last insert id


    "George Hill" <ghill@NOSPAM.c om> wrote in message
    news:LUf8b.5984 $ZB4.5409@reade r1.news.jippii. net...[color=blue]
    > Hi
    >
    > I am trying to import several master detail records from files to ms sql
    > server.
    > I have orders file and order_items file that has several rows for each
    > order.
    > If I insert programmaticall y these records how can find out which order ID
    > was the last inserted, so that I can attach the subsesquent row items to a
    > proper order.
    >
    > I am quite new to ms sql server. I have used mysql a lot and there I could
    > use mysql_insert_id to find out the last autoincremented filed number.
    > I am looking for a similar method for ms sql server 2000.
    >
    > TIA
    > George
    >
    >[/color]

    Assuming that you're using an IDENTITY column to generate the IDs, then the
    scope_identity( ) function will give the last ID inserted. There are also
    ident_current() and @@identity - see Books Online for an explanation - but
    scope_identity( ) is probably the one you want.

    Simon


    Comment

    Working...