T-SQL help

  • I have two identical tables. Table 1 has 12,000 rows and Table 2 has 11,000 rows. I need to insert rows from Table 2 on Table 1 that don’t exist on Table 1. What is a quick way of accomplishing this?

  • Insert into Table1 (<column list>)

    Select <columns> from Table2

    WHERE <primary key column> NOT IN (select <primary key column> FROM Table1)

    or

    Insert into Table1 (<column list>)

    Select <columns> from Table2

    EXCEPT

    Select <columns> from Table1

    Roughly.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • could also use something like:

    insert into table1 (Values)

    SELECT values

    FROM table2

    LEFT JOIN table1

    ON table2.somecolumn = table1.somecolumn

    WHERE table1.somecolumn IS NULL

    at lease i think since we dont have DDL and Sample data its hard to get tested code


    For faster help in answering any problems Please read How to post data/code on a forum to get the best help - Jeff Moden[/url] for the best way to ask your question.

    For performance Issues see how we like them posted here: How to Post Performance Problems - Gail Shaw[/url]

    Need to Split some strings? Jeff Moden's DelimitedSplit8K[/url]
    Jeff Moden's Cross tab and Pivots Part 1[/url]
    Jeff Moden's Cross tab and Pivots Part 2[/url]

  • Look at except:

    http://msdn.microsoft.com/en-us/library/ms188055.aspx

    Mark

  • Mark Eckeard (9/10/2012)


    Look at except:

    http://msdn.microsoft.com/en-us/library/ms188055.aspx

    Mark

    If you are going to post a url, at least make it usuable.

    http://msdn.microsoft.com/en-us/library/ms188055.aspx

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply