Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get the last INSERTED record

Status
Not open for further replies.

sreenath205

Programmer
Dec 9, 2003
17
IN


Sorry for creating confusion.Its a simple table with a primary key.I just need a query.
 
You need a field which is ordered by the insert - like an identity or timestamp or sequence set by a trigger.

If you are a multi-user system and want the record that your thread has just inserted then easiest is an identity with scope_identity. But you might want to have a look at the design and change things so that you know the PK of the rec you are inserting.


======================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
 
By logical if you inserting data into tablewith primary key then the data you have inserted is beeing maximum.
suppose that the table is 'customer' and 'customer_no' is promary key: then
select * from customer where customer_no=
(select max(customer_no) from customer)

in this way you are getting the last inserted record(if there has primary key)
 
>> By logical if you inserting data into tablewith primary key then the data you have inserted is beeing maximum.

Only if the PK is an increasing value e.g. an increasing identity or timestamp.
Doesn't work for guids for instance or decreasing identity.

Need more info about the system to give an answer.

======================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top