Quick Search for:  in language:    
   Code/Articles ╗ |  Newest/Best ╗ |  Community ╗ |  Jobs ╗ |  Other ╗ |  Goto ╗ | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
SQL Stats

 Code: 45,316. lines
 Jobs: 126. postings

 How to support the site

Sponsored by:

You are in:

Latest Code Ticker for SQL.
sp_db_paging_pr imary_key
By Marcus LeBlanc on 1/11

By Marcus LeBlanc on 1/11

Beginners pack
By hardik shah on 1/11

Inline Views
By Thivya Prabakaran on 1/6

Click here to put this ticker on your site!

Add this ticker to your desktop!

Daily Code Email
To join the 'Code of the Day' Mailing List click here!

Affiliate Sites


Filter duplicate records from a table

VB icon
Submitted on: 11/25/2003 3:10:06 PM
By: Geoff Kazzi 
Level: Advanced
User Rating: By 1 Users
Compatibility:SQL Server 2000, SQL Server 7.0, SQL Server 6.5 and earlier, Informix , Oracle

Users have accessed this code 2309 times.
(About the author)
     Deletes duplicate records from a table
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
Terms of Agreement:   
By using this code, you agree to the following terms...   
1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.

-- Name: Filter duplicate records from a
--      table
-- Description:Deletes duplicate records
--      from a table
-- By: Geoff Kazzi
-- Inputs:Database location 
SQL string identifying how TO obtain the UNIQUE records
-- Returns:nothing
--This code is copyrighted and has-- limited warranties.Please see http://
--     www.Planet-Source-Code.com/vb/scripts/Sh
--     owCode.asp?txtCodeId=786&lngWId;=5--for details.--**************************************

sSQLUniqueRecords = "SELECT ProductID, ProductName, SupplierID, CategoryID, QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued " & _ 
 " FROM Products " & _
 " GROUP BY ProductID, ProductName, SupplierID, CategoryID, QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued
'DB location AND the sql string that creates the UNIQUE SET OF records
PUBLIC Sub DeleteDuplicateRecords(sDBLocation AS String, sSQLUniqueRecords As String)
' Deletes duplicates FROM the specified table.
' No USER confirmation IS required. Use WITH caution.
Const TempTable AS String = " INTO tempTableDup FROM "
Const DEL_TEMPTABLE AS String = "DROP TABLE tempTableDup"
Dim rst AS Recordset
Dim ADOAction AS New ADODB.Connection
Dim sTable AS String
Dim i AS Long
Dim j AS Integer
Dim sInsert AS String
DBcnn = "DRIVER={Microsoft Access Driver (*.mdb)};" & _
"DBQ=" & sDBLocation & ";" & _
"DefaultDir=" & sDBLocation
' get the TABLE name 
sSQLUniqueRecords = UCase(sSQLUniqueRecords)
i = InStr(sSQLUniqueRecords, "FROM ") + 5
j = InStr(i, sSQLUniqueRecords, " ")
sTable = Mid(sSQLUniqueRecords, i, j - i)
' CREATE sql statement TO create TEMP TABLE 
sSQLUniqueRecords = Replace(sSQLUniqueRecords, " FROM " & sTable, TempTable & sTable)
sInsert = "INSERT INTO " & sTable & " SELECT * FROM tempTableDup"
ADOAction.Open DBcnn' open a connection
ADOAction.Execute sSQLUniqueRecords, i ' CREATE TEMP TABLE WITH unique records
ADOAction.Execute "DELETE FROM " & sTable' delete ALL the old records IN the table
ADOAction.Execute sInsert, i' UPDATE the TABLE WITH the UNIQUE records FROM TEMP table
ADOAction.Execute DEL_TEMPTABLE ' DROP the temp table
SET ADOAction = Nothing

Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Your Vote!

What do you think of this code(in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
Other User Comments

 There are no comments on this submission.
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.


Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | SQL Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyrightę 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.