0

Could any one help me in writing a MS SQL query to copy rows from one table to another only if table1.Col1 != table2.col1.

I am making a SSIS package to do this procedure so an approach related to it would be very helpful.

S7H
  • 1,421
  • 1
  • 22
  • 37
  • Possible duplicate of [SQL Server insert if not exists best practice](http://stackoverflow.com/questions/5288283/sql-server-insert-if-not-exists-best-practice) – Tab Alleman Jun 14 '16 at 12:30
  • Have you tried anything? Please show your effort in order for us to help you. – Denn Jun 14 '16 at 12:39

2 Answers2

1

1) Slowly Changing Dimension data flow task.

2) Use Lookup.

3) Use Merge.

BIDeveloper
  • 2,628
  • 4
  • 36
  • 51
sandeep rawat
  • 4,797
  • 1
  • 18
  • 36
1

Simplest way (to me): optionally join (LEFT OUTER JOIN) to the target table (table you're loading into), and use WHERE to confirm that the join fails:

INSERT INTO dbo.MYTARGETTABLE (TARG_COL1, TARG_COL2)
SELECT SRC.[SRC_COL1], SRC.[SRC_COL2]
FROM dbo.MYSOURCETABLE  SRC
    LEFT OUTER JOIN dbo.MYTARGETTABLE  TARG
        ON TARG.[TARG_COL1] = SRC.[SRC_COL1]
WHERE SRC.[SRC_COL1] IS NOT NULL
----this column is only null if the join failed: 
AND TARG.[TARG_COL1] IS NULL

This of course assumes that there are no valid NULLs in the column(s) you join with.