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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 how to fetch last entered records when i...

Author  Topic 

asifbhura
Posting Yak Master

165 Posts

Posted - 2006-06-19 : 02:59:51
Dear,

I have written query to select top3 records, as below

select top 3 c_name,c_add from customers

it is working ok.

but when i insert new records even though it doesnt fetch new inserted records,

I want top 3 records which are inserted last.

is there any query like?

select last 3 c_name,c_add from customer

i want only 3 records which are inserted last.

Please help me
regards,

jen
Master Smack Fu Yak Hacker

4110 Posts

Posted - 2006-06-19 : 03:02:42
if you have a datetime field you can use top or an incremental ID perhaps

otherwise, AFAIK the only way is via trigger

--------------------
keeping it simple...
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-06-19 : 03:12:09
Have a datetime column in your table to record the record creation date.

then use TOP 3 with ORDER BY ... DESC



KH

Go to Top of Page

sqldev80
Yak Posting Veteran

68 Posts

Posted - 2006-06-19 : 13:14:57
alter table tablename
ADD DATE datetime NULL
CONSTRAINT DATE_tablename
DEFAULT CONVERT(VARCHAR(10), GETDATE(), 126) with values

GO

select top 3 from tablename order by DATE DESC
Go to Top of Page

LoztInSpace
Aged Yak Warrior

940 Posts

Posted - 2006-06-19 : 21:28:45
You should use an identity because there is no guarantee that the date time will be unique. An identity will always give you a new value for each row.
Go to Top of Page
   

- Advertisement -