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 2005 Forums
 Transact-SQL (2005)
 Deleting all but first 6 rows?

Author  Topic 

OldMySQLUser
Constraint Violating Yak Guru

301 Posts

Posted - 2008-02-09 : 14:36:00
How can I delete all the rows in a table except the first six please?

cmspot
Starting Member

44 Posts

Posted - 2008-02-09 : 15:28:15
Delete from aaa where aakeyfield not in (Select top(6) from aaa)

I sell my mother in law.Is anybody interested?
Go to Top of Page

victory_vishnu
Starting Member

8 Posts

Posted - 2008-02-09 : 19:57:55
Test AS (SELECT ROW_NUMBER () OVER ( PARTITION BY ID, FNAME , LNAME ORDER BY ID) AS A FROM tbl1 ) DELETE FROM Test WHERE A > 6





My Attitude Is My Disguise ...
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-02-10 : 00:12:43
or this:-
DELETE t
FROM
(
SELECT ROW_NUMBER() OVER (ORDER BY KeyField) AS RowNo,
other fields...
FROM Table
)t
WHERE t.RowNo >6
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-02-10 : 05:46:35
SELECT TOP 6 * INTO #Temp FROM SourceTable ORDER BY SomeCol
TRUNCATE TABLE SourceTable

INSERT SourceTable SELECT * FROM #Temp
DROP TABLE #Temp




E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -