Home Forums SQL Server 2005 Administering remove rows from staging table that are duplicated before insert the data into the database normal tables RE: remove rows from staging table that are duplicated before insert the data into the database normal tables

  • One thing you could do is just use a SELECT DISTINCT when transferring the data, but if you want to remove duplicates I'd suggest this:

    WITH deDupe

    AS (

    SELECT

    BA.CODCTB,

    BA.NIF_ANTIGO,

    BA.NIF,

    BA.FILIAL_NUMBER,

    BA.INDEX_POS,

    BA.DESC_ACTIVIDADE,

    ROW_NUMBER() OVER (PARTITION BY BA.CODCTB, BA.NIF_ANTIGO,

    BA.NIF, BA.FILIAL_NUMBER, BA.INDEX_POS,

    BA.DESC_ACTIVIDADE ORDER BY BA.CODCTB) AS RowNo

    FROM

    dbo.BULK_ACTIVIDADES AS BA

    )

    DELETE FROM

    deDupe

    WHERE

    deDupe.RowNo > 1