Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4323760
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:02:38+00:00 2026-05-21T09:02:38+00:00

I am working on a document management system. Some documents were imported from another

  • 0

I am working on a document management system. Some documents were imported from another system. Due to an error, some of them were imported twice. I need to delete the duplicates. I have the document id from the previous system, but can’t just delete by that as some documents are associated with multiple accounts and are supposed to be in there twice, so I have to check against that as well. The associated values are in different tables. I have created the following script to come up with the doc id’s to delete but it is incredibly slow (it has been running for four days on a table with less than 2 million records).

declare @docidtodelete int
declare @docid int
declare @sourcedocid varchar(12)
declare @taxid decimal(9,0)
declare @account bigint


select @docid = MIN(d.docid) from DOCS d
inner join CONTENTS c on d.DOCID = c.DOCID and c.FOLID=1
while @docid is not null
      begin
            --get the source document id for this document
            select @sourcedocid = val from VTAB0031 where IDXID=31 and DOCID=@docid

            -- see if there is another document with the same source document id
            select @docidtodelete = isnull(MAX(v.docid),0) from VTAB0031 v
            inner join CONTENTS c on v.DOCID = c.DOCID and c.FOLID=1
            where IDXID=31 and VAL = @sourcedocid

            if @docid<@docidtodelete -- we have a possible duplicate so lets check and see if it matches on account
                  begin
                        select @account = val from VTAB0002 where IDXID=2 and DOCID=@docid
                        select @docidtodelete = isnull(max(v.docid),0) from VTAB0002 v
                              where IDXID=2 and VAL = @account and v.DOCID=@docidtodelete
                        if @docid<@docidtodelete -- we still have a possible duplicate so lets check and see if it matches on taxid
                        begin
                              select @taxid = val from VTAB0006 where IDXID=6 and DOCID=@docid
                              select @docidtodelete = isnull(max(v.docid),0) from VTAB0006 v
                                    where IDXID=6 and VAL = @taxid and v.DOCID = @docidtodelete
                              if @docid<@docidtodelete -- we still have a match so delete
                                                                begin
                                    insert into deletedDuplicates values(@docidtodelete ,@docid)
                                                                end
                        end
                  end
            select @docid = MIN(d.docid) from DOCS d
                  inner join CONTENTS c on d.DOCID = c.DOCID and c.FOLID=1
                  where d.DOCID > @docid
      end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T09:02:39+00:00Added an answer on May 21, 2026 at 9:02 am

    It’s always better to use set operations rather than procedural operations when working with an RDBMS.

    Try this instead:

    select  DocIdToDelete,
            DocIdToKeep
    into    deletedDuplicates
    from
    (
        select  max(DocId) as DocIdToDelete,
                min(DocId) as DocIdToKeep,
                SourceDocId,
                Account,
                TaxId,
                Count(*) as NumberMatches
        from
        (
            select  d.docid as DocId,
                    s.val as SourceDocId,
                    a.val as Account,
                    t.val as TaxId
            from    DOCS d
                    inner join CONTENTS c on c.DOCID = d.DOCID
                    inner join VTAB0031 s on s.DOCID = d.DOCID
                    inner join VTAB0002 a on a.DOCID = d.DOCID
                    inner join VTAB0006 t on t.DOCID = d.DOCID
            where   c.FOLID = 1
                    and s.IDXID = 31
                    and a.IDXID = 2
                    and t.IDXID = 6
        ) Summary
        group by    SourceDocId,
                    Account,
                    TaxId
        having  NumberMatches > 1
    ) Duplicates
    

    UPDATE

    I made a new query that should get all duplicate records. And this should run more effeciently as well, using the indexes.

    create table UniqueDocuments
    (
        DocId int not null,
        SourceDocId varchar(12) not null,
        Account bigint not null,
        TaxId decimal(9,0) not null
        primary key clustered (SourceDocId, Account, TaxId)
    )
    go
    
    insert into UniqueDocuments (DocId, SourceDocId, Account, TaxId)
    select  min(d.docid) as DocId,
            s.val as SourceDocId,
            a.val as Account,
            t.val as TaxId
    from    DOCS d
            inner join CONTENTS c on c.DOCID = d.DOCID
            inner join VTAB0031 s on s.DOCID = d.DOCID
            inner join VTAB0002 a on a.DOCID = d.DOCID
            inner join VTAB0006 t on t.DOCID = d.DOCID
    where   c.FOLID = 1
            and s.IDXID = 31
            and a.IDXID = 2
            and t.IDXID = 6
    group by s.val,
            a.val,
            t.val
    
    insert into DeletedDocuments (DocIdToDelete, DocIdToKeep)
    select  d.DocId as DocIdToDelete,
            ud.DocId as DocIdToKeep
    from    DOCS d
            inner join CONTENTS c on c.DOCID = d.DOCID
            inner join VTAB0031 s on s.DOCID = d.DOCID
            inner join VTAB0002 a on a.DOCID = d.DOCID
            inner join VTAB0006 t on t.DOCID = d.DOCID
            inner join UniqueDocuments ud on ud.SourceDocId = s.val
                                             and ud.Account = a.val
                                             and ud.TaxId = t.val
    where   c.FOLID = 1
            and s.IDXID = 31
            and a.IDXID = 2
            and t.IDXID = 6
            and d.DocId <> ud.DocId
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on moving a large number of documents from an older document management
I'm working on a document management system. An example workflow would be something like
I am working on a kinda document management system. The end users are business
I'm working with a customer that needs to migrate documents from their current document
I am working on a system that processes documents that are dumped into a
I have been working on a content management system (nakid) and one of my
I'm working on a document wizard for the company that I work for. It's
I'm working on a document application and part of this application I've to add
Working with a latex document with eps images as in the example below... \documentclass[11pt]{paper}
I'm working with an existing XML document which has a structure (in part) like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.