Keep from inserting duplicates based on 2 fields
I am new to SQL Server and am having a problem trying to figure out the best way to approach it.
I am working with SQL Server 2000.
My database has two tables of interest with fields that are 60% identical (that is, most of the
fields have the same data type and context, although they have different field names). For the purpose
of this inquiry, let me call the first table "tbl_INITIAL" and the second, "tbl_FINAL". Table
"tbl_INITIAL" is a temp table that is used to hold the contents of imported data for some
pre-processing. It is populated by a DTS package that imports a .CSV file. It is preferable that it not
be constrained until all pre-processing is finished.
"tbl_FINAL" has 10 of its 15 fields that have "identical" counterparts in "tbl_INITIAL". By
identical, I mean they have the same structure and context, but have different names. The remaining 5
fields in "tbl_FINAL" have no bearing on this discussion. "tbl_INITIAL" has records that I want to
"append" to "tbl_FINAL".
I need to make sure that no record in "tbl_INITIAL" with the "combination" of the following 3
fields, fld_FIRST, fld_SECOND, and fld_THIRD, are duplicated in "tbl_FINAL" during insertion or
modification. It is not important or desired to notify the user when one of these records is prohibited
from being inserted.
I was thinking of 3 approaches, all of which are new areas of learning for me:
1) Write a procedure (view, stored proc, or function???) to insert the records, but check for and
prevent "3-field combination" duplicates from being committed. I don't think this help when a record in
"tbl_FINAL" is modified.
2) Modify the structure of "tbl_FINAL" so that the combination of the 3 fields is UNIQUE. I don't know
what the consequences of this would be during batch updates. Would I get errors that would stop the
process, or would it just prevent the insert and keep going?
3) Build a trigger that would check for this condition on insert or modify and prevent the action if a
duplicate, 3-field combination shows up.
Needless to say, I don't know which approach is best OR how to write the code to do any them.
Can someone suggest which of the 3 or combination of the 3 approaches is the best way to go?
Could you point me to some examples?
Thanks ahead of time! :)
--- Tom
__________________
--- Tom
|