Article View: comp.databases.ms-sqlserver
Article #27Re: - Delete Multiple Rows?
From: don allon hinton
Date: Wed, 30 Oct 1996 00:00
Date: Wed, 30 Oct 1996 00:00
66 lines
2366 bytes
2366 bytes
assuming mytable has a unique primary key on column pk: set nocount on while exists (select col1 from mytable group by col1 having count(*)>1) begin select maxpk=max(pk),col1 into #dupes from mytable group by col1 having count(*)>1 delete mytable from mytable m, #dupes d where m.pk=d.maxpk drop table #dupes end set nocount off if mytable doesn't have a unique primary key: set nocount on declare @col1 int while exists (select col1 from mytable group by col1 having count(*)>1) begin select col1 into #dupes from mytable group by col1 having count(*)>1 while exists (select * from #dupes) begin set rowcount 1 select @col1=col1 from #dupes delete #dupes delete mytable where col1=@col1 set rowcount 0 end drop table #dupes end set nocount off hope this helps... don Les Gainous wrote: > > I'm using MS-SQL Server 6.5 (but any generic SQL syntax would be > appreciated): > > How can I delete multiple occurrences of a row within a table. I know > how to display the rows which are duplicated: > > select col1, > count(*) > from myTable > group by col1 > having count(*) > 1 > > But now that I have that listing, how can I delete the second (or third, > or...) occurrence of the row--preserving the first occurrence. > > Thanks in advance! > Les
Technical Details
Message-ID:
<32781B0B.1CBA@hypercon.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<3276F4A7.1842@earthlink.net>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f4bf8,e42b60ff2edc4f24 X-Google-Attributes: gidf4bf8,public X-Google-Thread: 11301e,e42b60ff2edc4f24 X-Google-Attributes: gid11301e,public X-Google-Thread: f6611,e42b60ff2edc4f24 X-Google-Attributes: gidf6611,public X-Google-Thread: 104de1,e42b60ff2edc4f24 X-Google-Attributes: gid104de1,public From: don allon hinton <dhinton@hypercon.com> Subject: Re: - Delete Multiple Rows? Date: 1996/10/30 Message-ID: <32781B0B.1CBA@hypercon.com> X-Deja-AN: 193402754 references: <3276F4A7.1842@earthlink.net> to: lesgainous@earthlink.net content-type: text/plain; charset=us-ascii organization: hinton & associates mime-version: 1.0 reply-to: dhinton@hypercon.com newsgroups: comp.databases.ms-sqlserver,comp.databases.theory,bit.databases.mssql-l,microsoft.public.sqlserver.programming x-mailer: Mozilla 3.0Gold (WinNT; I)