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
 How to retrieve bottom records from a table

Author  Topic 

leo1210
Starting Member

1 Post

Posted - 2009-07-16 : 13:04:04
hey everyone...plz reply as soon as possible

how can i retrieve or update the last three records from a table, which doesn't hav any primary key or row numbering and without using any stored proceedures...? (is there anything like 'top' keyword?)

qwerty

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-07-16 : 13:07:44
there's no concept of top or bottom in a sql table. you need to specify last or first based on order by means of order by clause. however, if you just want to update 3 random rows then use like below

UPDATE TOP (3) t
SET....
FROM Table1 t
....
WHERE ...

please note this works only from sql 2005 onwards
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-07-16 : 13:09:17
if using sql 2000, you can achieve the same as follows:-

SET ROWCOUNT 3

UPDATE t
SET....
FROM Table1 t
....
WHERE ..
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2009-07-16 : 13:09:37
How do you know which records are "the last three"? Last three alphabetically, last three inserted? etc.

Jim
Go to Top of Page
   

- Advertisement -