Connecting Tech Pros Worldwide Forums | Help | Site Map

Last insert id

George Hill
Guest
 
Posts: n/a
#1: Jul 20 '05
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 programmatically 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
Guest
 
Posts: n/a
#2: Jul 20 '05

re: Last insert id



"George Hill" <ghill@NOSPAM.com> wrote in message
news:LUf8b.5984$ZB4.5409@reader1.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 programmatically 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


Closed Thread