[dba-SQLServer] Delete all records of a particular type fromatable except for one copy.

Susan Harkins ssharkins at setel.com
Sun Jul 8 21:15:15 CDT 2007


 

How do you build a piece of SQL code that will use the Count aggregate
function to delete records?

=========Not sure you can. That's why I asked. :) I was just thinking -- can
you save the number of matching records to a variable. Also, can you run a
DELETE with TOP? If so, perhaps you could do something like 

countvar = SELECT Count(*) blah, blah, WHERE MyCode = value

DELETE TOP countvar - 1

I don't know if TOP with DELETE is allowed and if you went this route, you'd
need a sp to store countvar. This may be total nonsense in T-SQL. 

Susan H. 





More information about the dba-SQLServer mailing list