Seeing that you're using SQL Server 2008, I would recommend this approach:
- first bulkcopy your CSV files into a staging table
- update your target table from that staging table using the MERGE command
Check out the MSDN docs and a great blog post on how to use the MERGE command.
Basically, you create a link between your actual data table and the staging table on a common criteria (e.g. a common primary key), and then you can define what to do when
- the rows match, e.g. the row exists in both the source and the target table --> typically you'd either update some fields, or just ignore it all together
- the row from the source doesn't exist in the target --> typically a case for an INSERT
You would have a MERGE
statement something like this:
MERGE TargetTable AS t
USING SourceTable AS src
ON t.PrimaryKey = src.PrimaryKey
WHEN NOT MATCHED THEN
INSERT (list OF fields)
VALUES (list OF values)
WHEN MATCHED THEN
UPDATE
SET (list OF SET statements)
;
Of course, the ON
clause can be much more involved if needed. And of course, your WHEN
statements can also be more complex, e.g.
WHEN MATCHED AND (some other condition) THEN ......
and so forth.
MERGE
is a very powerful and very useful new command in SQL Server 2008 - use it, if you can!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…