I am having a brain fart on figuring this out. I have the following two Tables:
Table: parts
part_id INT IDENTITY(1,1) NOT NULL,
part_number VARCHAR(50) UNIQUE NOT NULL,
part_description VARCHAR(MAX) NOT NULL,
information VARCHAR(MAX) NULL,
manufacturer_id INT NOT NULL,
subcategory_id INT NOT NULL
Table: part_temp
part_num VARCHAR(50) NOT NULL,
part_desc VARCHAR(MAX) NULL,
info VARCHAR(MAX) NULL,
man_id INT NULL,
sub_id INT NULL
part_temp
is my temporary table that contains data from a CSV file, that is why only one column is set to be NOT NULL
. I need to insert the data from part_temp
into parts
.
I have properly cleaned the data in the table so there are no null values trying to be inserted into rows which require a value. My issue however is with my UNIQUE
constraint for my part_number
columns in the parts
table. There are duplicate values within the part_temp
table so I need a way to be able to skip over them during my insert. This is what I have tried so far, but it does not work:
INSERT INTO parts
SELECT DISTINCT pt.part_num, pt.part_desc, pt.info, m.manufacturer_id, s.subcategory_id
FROM part_temp AS pt
FULL OUTER JOIN man_temp AS mt ON pt.man_id = mt.man_id
INNER JOIN manufacturers AS m ON mt.man_name = m.manufacturer_name
FULL OUTER JOIN cat_temp AS ct ON pt.sub_id = ct.category_id
INNER JOIN subcategories AS s ON ct.category_name = s.subcategory_name
WHERE NOT EXISTS(SELECT part_number FROM parts WHERE part_number = pt.part_num)
These are the tables included in the joins not listed above
Table: man_temp
man_id INT NOT NULL,
man_name VARCHAR(100) NOT NULL
Table: manufacturers
manufacturer_id INT IDENTITY(1,1) NOT NULL,
manufacturer_name VARCHAR(100) NOT NULL
Table: cat_temp
category_id INT NOT NULL,
category_name VARCHAR(100) NOT NULL
Table: subcategories
subcategory_id INT IDENTITY(1,1) NOT NULL,
subcategory_name VARCHAR(100) NOT NULL
What is it that is wrong with my INSERT
query?
The specific error I am getting is:
Msg 2627, Level 14, State 1, Line 1
Violation of UNIQUE KEY constraint. Cannot insert duplicate key in object 'dbo.parts'. The duplicate key value is (31335A11)
part_num 31335A11
appears in the csv file more than once. As such it appears in the part_temp
table more than once. It would be easy if it was just this entry, but I have more than 1,000 repeat entries, so it would take forever to remove all the duplicates. Nothing exists in parts
as it is a brand new empty table I am trying to put values into.
DISTINCT
isn't helping because other attributes you are selecting out are making it non-unique and therefore looks to be a bog standard data quality issue. If you stick aWHERE partnum = '31335A11'
what output do you get?WHERE part_num = '31335A11'
I get the same error, but for the next duplicate item in the tableSELECT
and you should be able to visually see why31335A11
shows up twice - unless of course if it is an invisible control character in one of the text columns.