Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

xanburzum

Remover registros duplicados

Recommended Posts

<%@Language="vbscript" %>

<%

Const adOpenForwardOnly = 0

Const adLockReadOnly = 1

Const adCmdText = &H0001

Const adUseClient = 3

%>

<!DOCTYPE HTML Public "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML>

<HEAD>

<TITLE>Remover registros duplicados</TITLE>

<STYLE TYPE="text/css">

BODY { background-color: #336699; font-family: Verdana; font-size: 10pt; color: #FFFFFF }

TD { font-family: Verdana; font-size: 10pt; color: #000000 }

</STYLE>

</HEAD>

<BODY>

<%

sPassword = "admin" ' password

if Request("password") <> "" Then

Session("Password") = Request("password")

End if

if Session("Password") <> sPassword Then

Response.Write "<HTML><BODY><FORM ACTION=""removeduplicates.asp"" METHOD=""post"">Password: (CaSe SeNsiTivE) <INPUT TYPE=""password"" NAME=""password""><INPUT TYPE=""submit""></FORM>"

Else

delDub = False

File = Request.ServerVariables("SCRIPT_NAME")

File = StrReverse(File)

Posi = Instr(File, "/")

File = Left(File, Posi - 1)

File = StrReverse(File)

if Request.Form("db") <> "" Then

On Error Resume Next

DSNName = Request.Form("db")

sdbTable = Request.Form("tb")

spriKey = Request.Form("pk")

sdbField = Request.Form("fn")

sdbPath = "DBQ=" & Server.Mappath(DSNName) & ";Driver={Microsoft Access Driver (*.mdb)};"

strSQL = "SELECT COUNT(*) AS ttCount FROM " & sdbTable

Set strConn = Server.CreateObject("ADODB.Connection")

strConn.Open sdbPath

Set rs = Server.CreateObject("ADODB.Recordset")

rs.CursorLocation = adUseClient

rs.Open(strSQL), strConn, adOpenForwardOnly, adLockReadOnly, adCmdText

Set rs = strConn.Execute(strSQL)

tempArr = ""

totalFound = rs("ttCount")

rs.Close

Set rs = nothing

totalFound = CInt(totalFound)

Response.Write totalFound

Response.Write " records" & vbcrlf

Response.Write "<HR>" & vbcrlf

strSQL = "SELECT " & sdbField & "," & spriKey & " FROM " & sdbTable & " ORDER BY " & sdbField

Set rs = Server.CreateObject("ADODB.Recordset")

rs.CursorLocation = adUseClient

rs.Open(strSQL), strConn, adOpenForwardOnly, adLockReadOnly, adCmdText

if Err = 0 Then

For i = 0 To (totalFound - 1)

rs.MoveFirst

rs.Move(i)

lookFor = rs.fields(sdbField)

if lookFor = tempVal Then

foundDups = rs.fields(spriKey)

tempArr = tempArr & foundDups & ","

End if

tempVal = lookFor

Next

rs.Close

Set rs = nothing

if tempArr <> "" Then

Response.Write "Primary Key ID contendo dados duplicados no campo selecionado:" & vbcrlf

Response.Write "<BR>" & vbcrlf

End if

if tempArr = "" Then

Response.Write "Não há registros duplicados encontrados." & vbcrlf

Else

tempArr = Left(tempArr, Len(tempArr) - 1)

Response.Write tempArr

tempArr = Split(tempArr, ",", -1, 1)

prCount = UBound(tempArr)

prCount = prCount + 1

Response.Write "<BR>" & vbcrlf

For i = 0 To (prCount - 1)

strSQL = "DELETE FROM " & sdbTable & " WHERE " & spriKey & " = " & tempArr(i)

strConn.Execute(strSQL)

Next

End if

delDub = True

Else

Response.Write Err.Description

End if

strConn.Close

Set strConn = nothing

if Err = 0 And delDub = True Then

if tempArr <> "" Then

Response.Write "<BR><B>" & prCount & "</B> registro duplicado"

if prCount <> 1 Then

Response.Write "s"

End if

Response.Write " excluída com sucesso." & vbcrlf

End if

End if

End if

%>

<HR><BR><DIV ALIGN="center"><SPAN id="result_box"><SPAN title="Remove duplicate database records" closure_hashCode_1phoep="158">Remover duplicado registros de banco de dados</SPAN></SPAN></DIV>

<FORM ACTION="<%=File %>" METHOD="post">

<TABLE CELLPADDING="4" CELLSPACING="1" ALIGN="center" WIDTH="50%" STYLE="border: 1px outset" BGCOLOR="#FFFFFF">

<TR>

<TD WIDTH="50%" BGCOLOR="#E9E9E9">Database:</TD>

<TD WIDTH="50%" BGCOLOR="#E9E9E9">

<INPUT TYPE="text" SIZE="25" NAME="db" STYLE="text-align: right"><B>.mdb</B></TD></TR>

<TR>

<TD WIDTH="50%" BGCOLOR="#E9E9E9"> Nome da Tabela:</TD>

<TD WIDTH="50%" BGCOLOR="#E9E9E9">

<INPUT TYPE="text" SIZE="25" NAME="tb"></TD></TR>

<TR>

<TD WIDTH="50%" BGCOLOR="#E9E9E9">Nome do Campo:</TD>

<TD WIDTH="50%" BGCOLOR="#E9E9E9">

<INPUT TYPE="text" SIZE="25" NAME="fn"></TD></TR>

<TR>

<TD WIDTH="50%" BGCOLOR="#E9E9E9">Primary Key:</TD>

<TD WIDTH="50%" BGCOLOR="#E9E9E9">

<INPUT TYPE="text" SIZE="25" NAME="pk"></TD></TR>

<TR>

<TD WIDTH="50%" BGCOLOR="#E9E9E9"> </TD>

<TD WIDTH="50%" BGCOLOR="#E9E9E9">

<INPUT TYPE="submit" VALUE="Remove Duplicates"></TD></TR></TABLE></FORM>

<HR>

<%

End if

%>

</BODY>

</HTML>

Compartilhar este post


Link para o post
Compartilhar em outros sites

para sql server:

[left]Se houver alguma outra coluna é possível fazer via um único comando de DELETE.[/left]
 [left]Senão houver outra possibilidade é utilizar uma tabela temporária. Ex:[/left]
 [left] [/left]
  Code Snippet [left] [/left]
[size="2"][color="#0000ff"][size="2"][color="#0000ff"] [/color][/size][/color][/size][left][size="2"][color="#0000ff"][size="2"][color="#0000ff"]declare[/color][/size][/color][/size][size="2"] @t [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]table[/color][/size][/color][/size] [size="2"][color="#808080"][size="2"][color="#808080"]([/color][/size][/color][/size][size="2"]cod1 [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]int[/color][/size][/color][/size][size="2"][color="#808080"][size="2"][color="#808080"],[/color][/size][/color][/size][size="2"] cod2 [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]int[/color][/size][/color][/size][size="2"][color="#808080"][size="2"][color="#808080"])[/color][/size][/color][/size][/left]
[size="2"][color="#0000ff"][size="2"][color="#0000ff"] [/color][/size][/color][/size][left][size="2"][color="#0000ff"][size="2"][color="#0000ff"]insert[/color][/size][/color][/size] [size="2"][color="#0000ff"][size="2"][color="#0000ff"]into[/color][/size][/color][/size][size="2"] @t [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]values[/color][/size][/color][/size] [size="2"][color="#808080"][size="2"][color="#808080"]([/color][/size][/color][/size][size="2"]1[/size][size="2"][color="#808080"][size="2"][color="#808080"],[/color][/size][/color][/size][size="2"]2[/size][size="2"][color="#808080"][size="2"][color="#808080"])[/color][/size][/color][/size][/left]
[size="2"][color="#0000ff"][size="2"][color="#0000ff"] [/color][/size][/color][/size][left][size="2"][color="#0000ff"][size="2"][color="#0000ff"]insert[/color][/size][/color][/size] [size="2"][color="#0000ff"][size="2"][color="#0000ff"]into[/color][/size][/color][/size][size="2"] @t [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]values[/color][/size][/color][/size] [size="2"][color="#808080"][size="2"][color="#808080"]([/color][/size][/color][/size][size="2"]1[/size][size="2"][color="#808080"][size="2"][color="#808080"],[/color][/size][/color][/size][size="2"]3[/size][size="2"][color="#808080"][size="2"][color="#808080"])[/color][/size][/color][/size][/left]
[size="2"][color="#0000ff"][size="2"][color="#0000ff"] [/color][/size][/color][/size][left][size="2"][color="#0000ff"][size="2"][color="#0000ff"]insert[/color][/size][/color][/size] [size="2"][color="#0000ff"][size="2"][color="#0000ff"]into[/color][/size][/color][/size][size="2"] @t [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]values[/color][/size][/color][/size] [size="2"][color="#808080"][size="2"][color="#808080"]([/color][/size][/color][/size][size="2"]1[/size][size="2"][color="#808080"][size="2"][color="#808080"],[/color][/size][/color][/size][size="2"]2[/size][size="2"][color="#808080"][size="2"][color="#808080"])[/color][/size][/color][/size][/left]
[size="2"][color="#0000ff"][size="2"][color="#0000ff"] [/color][/size][/color][/size][left][size="2"][color="#0000ff"][size="2"][color="#0000ff"]insert[/color][/size][/color][/size] [size="2"][color="#0000ff"][size="2"][color="#0000ff"]into[/color][/size][/color][/size][size="2"] @t [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]values[/color][/size][/color][/size] [size="2"][color="#808080"][size="2"][color="#808080"]([/color][/size][/color][/size][size="2"]2[/size][size="2"][color="#808080"][size="2"][color="#808080"],[/color][/size][/color][/size][size="2"]1[/size][size="2"][color="#808080"][size="2"][color="#808080"])[/color][/size][/color][/size][/left]
 [left][size="2"][color="#808080"][size="2"][color="#808080"] [/color][/size][/color][/size][/left]
[size="2"][color="#008000"][size="2"][color="#008000"] [left]-- Insere os registros duplicados distintamente em outro local[/left]
[/color][/size][/color][/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"] [/color][/size][/color][/size][left][size="2"][color="#0000ff"][size="2"][color="#0000ff"]select[/color][/size][/color][/size][size="2"] cod1[/size][size="2"][color="#808080"][size="2"][color="#808080"],[/color][/size][/color][/size][size="2"] cod2 [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]into[/color][/size][/color][/size][size="2"] #t [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]from[/color][/size][/color][/size][size="2"] @t[/size][/left]
[size="2"][color="#0000ff"][size="2"][color="#0000ff"] [/color][/size][/color][/size][left][size="2"][color="#0000ff"][size="2"][color="#0000ff"]group[/color][/size][/color][/size] [size="2"][color="#0000ff"][size="2"][color="#0000ff"]by[/color][/size][/color][/size][size="2"] cod1[/size][size="2"][color="#808080"][size="2"][color="#808080"],[/color][/size][/color][/size][size="2"] cod2[/size][/left]
[size="2"][color="#0000ff"][size="2"][color="#0000ff"] [/color][/size][/color][/size][left][size="2"][color="#0000ff"][size="2"][color="#0000ff"]having[/color][/size][/color][/size] [size="2"][color="#ff00ff"][size="2"][color="#ff00ff"]count[/color][/size][/color][/size][size="2"][color="#808080"][size="2"][color="#808080"](*)[/color][/size][/color][/size] [size="2"][color="#808080"][size="2"][color="#808080"]>[/color][/size][/color][/size][size="2"] 1[/size][/left]
 [left] [/left]
[size="2"][color="#008000"][size="2"][color="#008000"] [left]-- Delete os registros duplicados (todos)[/left]
[/color][/size][/color][/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"] [/color][/size][/color][/size][left][size="2"][color="#0000ff"][size="2"][color="#0000ff"]delete[/color][/size][/color][/size] [size="2"][color="#0000ff"][size="2"][color="#0000ff"]from[/color][/size][/color][/size][size="2"] @t[/size][/left]
[size="2"][color="#0000ff"][size="2"][color="#0000ff"] [/color][/size][/color][/size][left][size="2"][color="#0000ff"][size="2"][color="#0000ff"]from[/color][/size][/color][/size][size="2"] @t [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]as[/color][/size][/color][/size][size="2"] t[/size][/left]
[size="2"][color="#808080"][size="2"][color="#808080"] [/color][/size][/color][/size][left][size="2"][color="#808080"][size="2"][color="#808080"]inner[/color][/size][/color][/size] [size="2"][color="#808080"][size="2"][color="#808080"]join[/color][/size][/color][/size] [size="2"][color="#808080"][size="2"][color="#808080"]([/color][/size][/color][/size][/left]
[size="2"][color="#0000ff"][size="2"][color="#0000ff"] [/color][/size][/color][/size][left][size="2"][color="#0000ff"][size="2"][color="#0000ff"]select[/color][/size][/color][/size][size="2"] cod1[/size][size="2"][color="#808080"][size="2"][color="#808080"],[/color][/size][/color][/size][size="2"] cod2 [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]from[/color][/size][/color][/size][size="2"] @t[/size][/left]
[size="2"][color="#0000ff"][size="2"][color="#0000ff"] [/color][/size][/color][/size][left][size="2"][color="#0000ff"][size="2"][color="#0000ff"]group[/color][/size][/color][/size] [size="2"][color="#0000ff"][size="2"][color="#0000ff"]by[/color][/size][/color][/size][size="2"] cod1[/size][size="2"][color="#808080"][size="2"][color="#808080"],[/color][/size][/color][/size][size="2"] cod2[/size][/left]
[size="2"][color="#0000ff"][size="2"][color="#0000ff"] [/color][/size][/color][/size][left][size="2"][color="#0000ff"][size="2"][color="#0000ff"]having[/color][/size][/color][/size] [size="2"][color="#ff00ff"][size="2"][color="#ff00ff"]count[/color][/size][/color][/size][size="2"][color="#808080"][size="2"][color="#808080"](*)[/color][/size][/color][/size] [size="2"][color="#808080"][size="2"][color="#808080"]>[/color][/size][/color][/size][size="2"] 1[/size][size="2"][color="#808080"][size="2"][color="#808080"])[/color][/size][/color][/size] [size="2"][color="#0000ff"][size="2"][color="#0000ff"]as[/color][/size][/color][/size][size="2"] q[/size][/left]
[size="2"][color="#0000ff"][size="2"][color="#0000ff"] [/color][/size][/color][/size][left][size="2"][color="#0000ff"][size="2"][color="#0000ff"]on[/color][/size][/color][/size][size="2"] t[/size][size="2"][color="#808080"][size="2"][color="#808080"].[/color][/size][/color][/size][size="2"]cod1 [/size][size="2"][color="#808080"][size="2"][color="#808080"]=[/color][/size][/color][/size][size="2"] q[/size][size="2"][color="#808080"][size="2"][color="#808080"].[/color][/size][/color][/size][size="2"]cod1 [/size][size="2"][color="#808080"][size="2"][color="#808080"]and[/color][/size][/color][/size][size="2"] t[/size][size="2"][color="#808080"][size="2"][color="#808080"].[/color][/size][/color][/size][size="2"]cod2 [/size][size="2"][color="#808080"][size="2"][color="#808080"]=[/color][/size][/color][/size][size="2"] q[/size][size="2"][color="#808080"][size="2"][color="#808080"].[/color][/size][/color][/size][size="2"]cod2[/size][/left]
 [left] [/left]
[size="2"][color="#008000"][size="2"][color="#008000"] [left]-- Reinsere os registros distintamente[/left]
[/color][/size][/color][/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"] [/color][/size][/color][/size][left][size="2"][color="#0000ff"][size="2"][color="#0000ff"]insert[/color][/size][/color][/size] [size="2"][color="#0000ff"][size="2"][color="#0000ff"]into[/color][/size][/color][/size][size="2"] @t [/size][size="2"][color="#808080"][size="2"][color="#808080"]([/color][/size][/color][/size][size="2"]cod1[/size][size="2"][color="#808080"][size="2"][color="#808080"],[/color][/size][/color][/size][size="2"] cod2[/size][size="2"][color="#808080"][size="2"][color="#808080"])[/color][/size][/color][/size][/left]
[size="2"][color="#0000ff"][size="2"][color="#0000ff"] [/color][/size][/color][/size][left][size="2"][color="#0000ff"][size="2"][color="#0000ff"]select[/color][/size][/color][/size][size="2"] cod1[/size][size="2"][color="#808080"][size="2"][color="#808080"],[/color][/size][/color][/size][size="2"] cod2[/size][/left]
[size="2"][color="#0000ff"][size="2"][color="#0000ff"] [/color][/size][/color][/size][left][size="2"][color="#0000ff"][size="2"][color="#0000ff"]from[/color][/size][/color][/size][size="2"] #t[/size][/left]
[size="2"][color="#0000ff"][size="2"][color="#0000ff"] [/color][/size][/color][/size][left][size="2"][color="#0000ff"][size="2"][color="#0000ff"]select[/color][/size][/color][/size] [size="2"][color="#808080"][size="2"][color="#808080"]*[/color][/size][/color][/size] [size="2"][color="#0000ff"][size="2"][color="#0000ff"]from[/color][/size][/color][/size][size="2"] @t[/size][/left]
 [left] [/left]
[size="2"][color="#0000ff"][size="2"][color="#0000ff"] [/color][/size][/color][/size][left][size="2"][color="#0000ff"][size="2"][color="#0000ff"]drop[/color][/size][/color][/size] [size="2"][color="#0000ff"][size="2"][color="#0000ff"]table[/color][/size][/color][/size][size="2"] #t[/size][/left]

fonte: http://social.msdn.microsoft.com/Forums/pt-BR/transactsqlpt/thread/8a535a8d-db5a-496f-94c6-66449c487d05

Compartilhar este post


Link para o post
Compartilhar em outros sites

um outro exemplo

 

<%@ LANGUAGE = "VBScript" %>
    <% Option Explicit %>
    <%server.scripttimeout=600%>
       <% 
       Dim mydatabase,mytable,myprimarykey,myfield
    mydatabase="../../db/signets.mdb"
    mytable="mes_sites"
    myprimarykey="id"
    myfield="url"
    Dim SQL,conn,rs,nb,i,nbtot,valurl,nbdup,nbdup2,valret,validdup,arr,nbarr
    Set Conn = Server.CreateObject("ADODB.Connection")
    Set rs = Server.CreateObject("ADODB.Recordset")
    Conn.Open "DBQ=" & Server.Mappath(mydatabase) & ";Driver={Microsoft Access Driver (*.mdb)};"
    %>
    <% 
    sql="select count(*) as nb from " 
    sql=sql & mytable
    Set RS = conn.Execute(SQL)
      arr=""
    nbtot=rs("nb")
    nbtot=cint(nbtot)
    response.write nbtot
    response.write " fiches / files"
    response.write "<HR>"
    sql="select "
    sql = sql & myfield
    sql=sql & ","
    sql=sql & myprimarykey
    sql=sql & " from "
    sql=sql & mytable
    sql=sql & " order by "
    sql=sql & myfield
    Set rs=conn.execute(sql) 
    For i=0 To (nbtot- 1)
    rs.movefirst
    rs.move(i)
    valurl= rs.fields(myfield)
      if valurl=valret Then
    validdup= rs.fields(myprimarykey)
      arr= arr & validdup
    arr = arr & ","
    else
    End if

    valret = valurl
    next
    rs.close
    Set rs=nothing
       response.write "Os registros que contêm dados duplicados em campo myfield"
    response.write "<BR>"

    if arr = "" Then
    response.write "Não há registro duplicado."
    else
    arr=left(arr,len(arr)-1)
    response.write arr
    arr=split(arr,",",-1,1)
    nbarr = ubound(arr) 
    nbarr=nbarr + 1
    response.write "<BR>"
    response.write "Número de registros duplicados:"
    response.write nbarr
    response.write "<BR>"

    For i=0 To nbarr-1
    SQL = "delete from "
    sql=sql & mytable
    SQL = SQL & " WHERE "
    sql=sql & myprimarykey 
    sql = sql & " =" 
    sql=sql & arr(i)
    Set RS = conn.Execute(SQL)
    next
    Response.write "Todos os registros duplicados são deletados."
    End if
    Set rs=nothing
    conn.close
    Set conn = nothing
    %>

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.