INSERT but ignore duplicates

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • shorti

    INSERT but ignore duplicates

    I am looking for a way to insert into one table from another table but
    ignore duplicates (because the query will fail since the column I am
    inserting into is a unique index).

    RE:

    INSERT INTO table1 (SELECT column1 from table2)

    This query works if table1 is empty but if table1 has any recorda they
    would be duplicate to table2. I also cannot delete the existing
    records in table1. I did not see anything like an IGNORE clause and I
    really would like to avoid opening my own cursor to go through the
    table. Any suggestions?

  • Mark Yudkin

    #2
    Re: INSERT but ignore duplicates

    Use the MERGE statement. It's designed for exactly what you want.

    "shorti" <lbryan21@juno. com> wrote in message
    news:1146805431 .592291.238640@ e56g2000cwe.goo glegroups.com.. .[color=blue]
    >I am looking for a way to insert into one table from another table but
    > ignore duplicates (because the query will fail since the column I am
    > inserting into is a unique index).
    >
    > RE:
    >
    > INSERT INTO table1 (SELECT column1 from table2)
    >
    > This query works if table1 is empty but if table1 has any recorda they
    > would be duplicate to table2. I also cannot delete the existing
    > records in table1. I did not see anything like an IGNORE clause and I
    > really would like to avoid opening my own cursor to go through the
    > table. Any suggestions?
    >[/color]


    Comment

    • Brian Tkatch

      #3
      Re: INSERT but ignore duplicates

      > I did not see anything like an IGNORE clause

      WHERE means ignore anything that doesn't satisfy the clause.

      INSERT INTO table1 (SELECT DISTINCT column1 from table2
      WHERE NOT EXISTS (SELECT * FROM table1 WHERE column1 = table2.column1) )

      Though, you could also use MERGE.

      B.

      Comment

      • shorti

        #4
        Re: INSERT but ignore duplicates

        Ok..thanks! I will try them out!

        Comment

        Working...