Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 gettting last inserted record

Author  Topic 

sqllover
Constraint Violating Yak Guru

338 Posts

Posted - 2007-02-09 : 05:23:28
hi,i need to select last inserted record in my table,can any one show some example query for that please

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-02-09 : 05:31:11
Not possible unless you are recording the datetime when record was inserted or using rowversion column in the table.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

sqllover
Constraint Violating Yak Guru

338 Posts

Posted - 2007-02-09 : 05:47:39
hi harsh_athalye i am javing updated date as datecolumn in my database.so now cani get lastinserted record in my table please show some example

my table has the following columns:

name id updated_date
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-02-09 : 05:50:40
[code]Select TOP 1 * from tbl
order by Updated_date DESC
[/code]

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

sqllover
Constraint Violating Yak Guru

338 Posts

Posted - 2007-02-09 : 06:12:59
thanks harsh_athalye i did that
Go to Top of Page

a4nsd
Starting Member

20 Posts

Posted - 2007-02-11 : 02:10:33
Hi sqllover
If you id field in table and set autocreament =1 & primary key. U can try this
select * from tbl_Products Where ProductID=(Select Max(ProductID) from tbl_Products)
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-02-11 : 05:27:23
"If you id field in table and set autocreament =1 "
This may be faster

SELECT TOP 1 Col1, Col2, ...
FROM tbl_Products
ORDER BY ProductID DESC

Kristen
Go to Top of Page
   

- Advertisement -