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
 In a table i need to delete some rows

Author  Topic 

subashreddy
Starting Member

6 Posts

Posted - 2011-05-17 : 05:19:58
Can any one help me how to delete all rows except 4 rows..
Example: In a Employee table there are 100 records in that i want to delete 96 records and i want to keep 4 records in employee table..can any one help me please

subash

Ranjit.ileni
Posting Yak Master

183 Posts

Posted - 2011-05-17 : 05:38:16
which four records(top, bottom.middle,...etc.,) you want to keep (out of 100)


--Ranjit
Go to Top of Page

Ranjit.ileni
Posting Yak Master

183 Posts

Posted - 2011-05-17 : 05:42:28
Refere Examples from

http://msdn.microsoft.com/en-us/library/ms189835(v=SQL.90).aspx

--Ranjit
Go to Top of Page

subashreddy
Starting Member

6 Posts

Posted - 2011-05-17 : 05:43:29
Thanks for your early reply,

i want to keep middle four records

subash
Go to Top of Page

subashreddy
Starting Member

6 Posts

Posted - 2011-05-17 : 05:56:00
Ranjit,

Can u please help me,

--Delete from employee where

after that i what condition i need to keep,

I want to keep middle four records

please help me.

subash
Go to Top of Page

Ranjit.ileni
Posting Yak Master

183 Posts

Posted - 2011-05-17 : 06:21:01
Check This Example:


create table #temp
(
number int identity(1,1)
,name varchar(10)
)
go


-- Inserting 100 Records in a single batch
insert into #temp(name) values('aaaaa')
go 100

select * from #temp
go



--Deleting records using row_number() function
with cte as
(
select number,name,row_number() over(order by number)as serialNum from #temp
)delete from cte --deleteing records
where serialnum not in (49,50,51,52)



--Ranjit
Go to Top of Page

subashreddy
Starting Member

6 Posts

Posted - 2011-05-18 : 00:39:53
Thank you Ranjit

subash
Go to Top of Page

Ranjit.ileni
Posting Yak Master

183 Posts

Posted - 2011-05-18 : 06:24:29
Welcome subashreddy

--Ranjit
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-05-18 : 09:43:45
Welcome for what?

It's just been scary around here lately



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2011-05-18 : 14:19:18
quote:
Originally posted by X002548

Welcome for what?

It's just been scary around here lately

<snip huge sig>
Maybe for a solution?
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-05-18 : 14:46:31
like a shotgun

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -