Groups | Blog | Home
all groups > sql server programming > february 2004 >

sql server programming : How do I deleting all but the last 3 records entered


Al
2/27/2004 2:56:05 AM
If you have identical rows, you can limit the number you delete by usin

SET ROWCOUNT

where X is the number of rows you want to delete. If you if have 3 and want to delete 1

SET ROWCOUNT 1
DELETE ........
SET ROWCOUNT

Immy
2/27/2004 10:36:22 AM
Try using a trigger if you have a date or id field!
[quoted text, click to view]

Tibor Karaszi
2/27/2004 11:31:05 AM
Do you have some column which specify the sequence of rows inserted or date
when rows were inserted?

--
Tibor Karaszi, SQL Server MVP
Archive at:
http://groups.google.com/groups?oi=djq&as_ugroup=microsoft.public.sqlserver


[quoted text, click to view]

Tibor Karaszi
2/27/2004 11:46:11 AM
Then you can do something like below inside your trigger (code not tested
because no DDL):

DELETE FROM tblname AS o
WHERE NOT EXISTS
(SELECT TOP 3 * FROM tblname AS i
WHERE o.pk = i.pk
ORDER BY DateCreated DESC)

Or use an IN instead.

--
Tibor Karaszi, SQL Server MVP
Archive at:
http://groups.google.com/groups?oi=djq&as_ugroup=microsoft.public.sqlserver


[quoted text, click to view]

Piotr Hamrol
2/27/2004 11:55:48 AM
delete for this member all records without last 2 records using

SELECT TOP 2 ... in WHERE condition in DELETE

and next add this new record

ph


U¿ytkownik "SamIAm" <samuel@rubbachicken.com> napisa³ w wiadomo¶ci
news:eySxXmR$DHA.3188@TK2MSFTNGP09.phx.gbl...
[quoted text, click to view]

Roji. P. Thomas
2/27/2004 4:19:05 PM
IF (SELECT COUNT(*) FROM YourTable WHERE MemberId = @intmemberid) >= 3
DELETE FROM YourTable WHERE MemberId = @intmemberid
AND DateCreated = SELECT MIN(DateCreated FROM YourTable WHERE MemberId =
@intmemberid)

INSERT ....


--
Roji. P. Thomas
SQL Server Programmer
[quoted text, click to view]

Roji. P. Thomas
2/27/2004 4:38:31 PM
Tibor,

I believe the query u suggested will not work.

The expression
EXISTS
(SELECT TOP 3 * FROM tblname AS i
WHERE o.pk = i.pk
ORDER BY DateCreated DESC)

Will evaluate to TRUE even if you have only one record.
Right?



--
Roji. P. Thomas
SQL Server Programmer
"Tibor Karaszi" <tibor_please.no.email_karaszi@hotmail.nomail.com> wrote in
message news:exBzq7R$DHA.3712@tk2msftngp13.phx.gbl...
[quoted text, click to view]

SamIAm
2/27/2004 9:08:25 PM
Hi

I have a system where each member can only have 3 records in a table. When a
new row is created for a member, I need to delete the oldest record first so
that there are never more than 3 records for a member.

How do I do this in TSQL?

Thank,

S

SamIAm
2/27/2004 9:41:21 PM
I have a DateCreated column

"Tibor Karaszi" <tibor_please.no.email_karaszi@hotmail.nomail.com> wrote in
message news:OZlGPzR$DHA.1796@TK2MSFTNGP12.phx.gbl...
[quoted text, click to view]

SamIAm
2/27/2004 9:41:31 PM
I have a DateCreated column

[quoted text, click to view]

SamIAm
2/27/2004 10:39:28 PM
Thanks


[quoted text, click to view]

AddThis Social Bookmark Button