Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 16th, 2004, 04:18 PM
Authorized User
 
Join Date: Jun 2003
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default Simply Deleting Question - ThnX !

Hi,

Easy sql deleting question - I hope.

I have a duplicate record in a table, and I would only want to delete one of them.

Let's say the record is this:

perid name place thing

1 Kris HK apple
1 Kris HK apple

Thanks in advance for your help !
 
Old November 16th, 2004, 04:20 PM
Authorized User
 
Join Date: Jun 2003
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Opps. Forgot to mention that this is for MS SQL 2K.
 
Old November 16th, 2004, 04:55 PM
Authorized User
 
Join Date: Jun 2003
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I found my answer:

DECLARE dupsCsr CURSOR READ_ONLY FOR

SELECT COUNT(*), fld_Date, fld_Name, fld_Item
FROM tblMyTable
GROUP BY fld_Date, fld_Name, fld_Item
ORDER BY COUNT(*) DESC

DECLARE @emso INT --Change to match datatype on your table
DECLARE @numDups INT

OPEN dupsCsr
FETCH NEXT FROM dupsCsr INTO @emso, @numDups
WHILE @@FETCH_STATUS = 0
BEGIN
     SET @numDups = @numDups - 1 --delete all but 1 of the duplicates
     SET ROWCOUNT @numDups
     DELETE FROM yourTable
     WHERE emso = @emso
     FETCH NEXT FROM dupsCsr INTO @emso, @numDups
END --WHILE
CLOSE dupsCsr
DEALLOCATE dupsCsr

SET ROWCOUNT 0 --restore default





Similar Threads
Thread Thread Starter Forum Replies Last Post
Name a range simply for each third cell wapfu Excel VBA 2 December 6th, 2006 03:04 PM
Problem with a very simply code: Dango24 BOOK: Beginning ASP.NET 1.0 13 August 4th, 2004 03:50 PM
Response To a Javascript - ThnX ! ank2go ASP.NET 1.0 and 1.1 Professional 14 July 12th, 2004 05:24 PM
Anchor Problem - Plz Help - ThnX ! ank2go HTML Code Clinic 6 June 30th, 2004 03:18 PM
Date Format - Truncating - Plz Help - THNX ! ank2go VB.NET 2002/2003 Basics 2 March 30th, 2004 11:26 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.