Snitz Forums 2000
Snitz Forums 2000
Home | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Help Groups for Snitz Forums 2000 Users
 Help: Database: MS SQL Server
 Messed up archive
 New Topic  Topic Locked
 Printer Friendly
Author Previous Topic Topic Next Topic  

DavidRhodes
Senior Member

United Kingdom
1222 Posts

Posted - 27 November 2003 :  08:24:40  Show Profile
Whilst archiving a forum my browser hung, I shut it down and tried again and it went ok. Thought nothing of it until I search the archive and found that each thread had 2 replied the same! My first attempt must have copied them across and crashed before deleting them from the FORUM_REPLY table therefore been copied again on my second attempt.

I can't figure out how I can clean this up without going though the db one by one! I have EM so can run SQL script if anyone can lend a hand?

The UK MkIVs Forum

HuwR
Forum Admin

United Kingdom
20595 Posts

Posted - 27 November 2003 :  09:18:26  Show Profile  Visit HuwR's Homepage
if the table doesn't have a unique id field, then add one and do


SELECT A1.*
from FORUM_A_REPLY A1, FORUM_A_REPLY A2
where A1.REPLY_ID = A2.REPLY_ID
AND A1.UID <> A2.UID
Go to Top of Page

HuwR
Forum Admin

United Kingdom
20595 Posts

Posted - 27 November 2003 :  09:27:00  Show Profile  Visit HuwR's Homepage
Ok, this should help a bit more


Alter table FORUM_A_REPLY add NewPK int NULL
go

-- populate the new Primary Key
declare @intCounter int
set @intCounter = 0
update FORUM_A_REPLY 
SET @intCounter = NewPK = @intCounter + 1

-- ID the records to delete and get one primary key value also
-- We'll delete all but this primary key
select REPLY_ID, RecCount=count(*), PktoKeep = max(NewPK)
into #dupes
from FORUM_A_REPLY 
group by REPLY_ID
having count(*) > 1
order by count(*) desc, REPLY_ID

-- delete dupes except one Primary key for each dup record
delete	*
from	FORUM_A_REPLY a join #dupes d
on	d.reply_id = a.reply_id
where	a.NewPK not in (select PKtoKeep from #dupes)

-- remove the NewPK column
ALTER TABLE FORUM_A_REPLY DROP COLUMN NewPK
go

drop table #dupes
Go to Top of Page

DavidRhodes
Senior Member

United Kingdom
1222 Posts

Posted - 27 November 2003 :  09:40:24  Show Profile
cheers
i get an incorrect syntax error near * line 18
and #dupe doesn't exist

The UK MkIVs Forum
Go to Top of Page

HuwR
Forum Admin

United Kingdom
20595 Posts

Posted - 27 November 2003 :  10:05:37  Show Profile  Visit HuwR's Homepage
change this line
delete *
to just
delete FORUM_A_REPLY

do you mean #dupe or #dupes ??
Go to Top of Page

DavidRhodes
Senior Member

United Kingdom
1222 Posts

Posted - 27 November 2003 :  11:45:57  Show Profile
sorry, meant #dupes

The UK MkIVs Forum
Go to Top of Page

Jeepaholic
Average Member

USA
697 Posts

Posted - 27 November 2003 :  12:15:09  Show Profile  Visit Jeepaholic's Homepage
That's the exact script I used to remove my duplicates last week, works great. Only difference (and probably not required) is that I included CAT_ID, TOPIC_ID, and FORUM_ID in determining duplicates (since they are all used to define the PK).

FYI David, I built SQL-based Archive and Un-Archive scripts here...I was having time-out issues with the Snitz Archiving scripts (resulting in similar duplicates that you had), so needed to generate something that ran quicker.

ARCHIVE
http://forum.snitz.com/forum/topic.asp?TOPIC_ID=47702

UN-ARCHIVE
http://forum.snitz.com/forum/topic.asp?TOPIC_ID=49367

Al Bsharah
Aholics.com

Jeepaholics Anonymous
Broncoholics Anonymous
Network Insight
Go to Top of Page

HuwR
Forum Admin

United Kingdom
20595 Posts

Posted - 27 November 2003 :  12:36:29  Show Profile  Visit HuwR's Homepage
quote:
Originally posted by DavidRhodes

sorry, meant #dupes




You shouldn't be getting any errors for that, it is a temp table.
Go to Top of Page

DavidRhodes
Senior Member

United Kingdom
1222 Posts

Posted - 27 November 2003 :  13:34:28  Show Profile
#dupes is not erroring now.
I have got up to the delete statement, eg #dupes is populated, but i'm getting an error on
-- delete dupes except one Primary key for each dup record
delete	
from	FORUM_A_REPLY a join #dupes d
on	d.reply_id = a.reply_id
where	a.NewPK not in (select PKtoKeep from #dupes)

You can't delete on joins can you? The error is before the word join

The UK MkIVs Forum

Edited by - DavidRhodes on 27 November 2003 13:55:05
Go to Top of Page

DavidRhodes
Senior Member

United Kingdom
1222 Posts

Posted - 27 November 2003 :  14:07:41  Show Profile
I'm not sure that delete statement would be correct?
I have ran the query that Huw posted first and all the duplicates have the same value for NewPK, eg looking down the query results it displays 1,1,2,2,3,3,4,4,5,5,6,6 and so on.
Non of the values in #dupes.PKtoKeep match any values in FORUM_A_REPLY.NewPK so the statement "where a.NewPK not in (select PKtoKeep from #dupes)" would be pointless?

The UK MkIVs Forum
Go to Top of Page

HuwR
Forum Admin

United Kingdom
20595 Posts

Posted - 27 November 2003 :  14:10:28  Show Profile  Visit HuwR's Homepage
quote:
Originally posted by HuwR

change this line
delete *
to just
delete FORUM_A_REPLY

do you mean #dupe or #dupes ??



did you change the

delete * to delete FORUM_A_REPLY ?

the logic for the code is correct, #dupes has the newkey for the ones you are keeping, that is why you are deleting the one not in #dupes
Go to Top of Page

DavidRhodes
Senior Member

United Kingdom
1222 Posts

Posted - 27 November 2003 :  14:15:50  Show Profile
I tried
delete from FORUM_A_REPLY a join #dupes d
on	d.reply_id = a.reply_id
where	a.NewPK not in (select PKtoKeep from #dupes)

Don't you still need the "FROM"?
I tried without the FROM and got the same error

The UK MkIVs Forum
Go to Top of Page

HuwR
Forum Admin

United Kingdom
20595 Posts

Posted - 27 November 2003 :  14:36:28  Show Profile  Visit HuwR's Homepage
no, just change the delete * line to delete FORUM_A_REPLY, do not change anything else.
Go to Top of Page

DavidRhodes
Senior Member

United Kingdom
1222 Posts

Posted - 27 November 2003 :  14:42:09  Show Profile
that worked, thank you

I never new you could do DELETE <table_name> FROM.........

The UK MkIVs Forum
Go to Top of Page

DavidRhodes
Senior Member

United Kingdom
1222 Posts

Posted - 27 November 2003 :  14:44:30  Show Profile
quote:
Originally posted by HuwR

if the table doesn't have a unique id field, then add one and do


SELECT A1.*
from FORUM_A_REPLY A1, FORUM_A_REPLY A2
where A1.REPLY_ID = A2.REPLY_ID
AND A1.UID <> A2.UID




UID doesn't exist in FORUM_A_REPLY?

The UK MkIVs Forum
Go to Top of Page

HuwR
Forum Admin

United Kingdom
20595 Posts

Posted - 27 November 2003 :  14:51:18  Show Profile  Visit HuwR's Homepage
the second post superceded the first, just ignore that post.

UID would have refered to "if the table doesn't have a unique id field, then add one"
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Topic Locked
 Printer Friendly
Jump To:
Snitz Forums 2000 © 2000-2021 Snitz™ Communications Go To Top Of Page
This page was generated in 0.19 seconds. Powered By: Snitz Forums 2000 Version 3.4.07