2

I currently have two tables. One fairly large and one suitably small. The large table is made up of many inserts from tables in the same style as the small one.

I want to insert (probably using insert into) the records from the small table into the big table IF they are not already present in the big table.

Points of Note

1
  • 1
    4th point of note should be that you're offering a bounty for slapping the moron who designed such a table. No primary key? Good luck hacking this, I know I'd beat the living s*** out of the idiot who made me work with such a mess. Commented Aug 19, 2015 at 11:26

1 Answer 1

1

You can do this with not exists:

insert into bigTable(col1, . . ., coln)
   select col1, . . ., coln
   from smallTable s
   where not exists (select 1
                     from bigTable b
                     where b.col1 = s.col1 and b.col2 = s.col2 and . . .
                    );

Note: this does not work for NULL values. Well, it does "work", but you will get duplicate rows, which is consistent with the meaning of NULL as "unknown" (as opposed to "missing").

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, that worked perfectly (and the hint about NULL results is very useful as well!)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.