Hi, I have two tables both containing data under the same column headings and types, code and name. The name column is a unique index with an insert and update constraint.
I am trying to insert data from table2 to table1 when the data does not already exist in table1 but am getting a unique key constraint error (cannot insert duplicate key). My code is as follows:
Curiously, I can use the following, practically identical, code to copy the data one row at a time with no error:
Any ideas?
DT
I am trying to insert data from table2 to table1 when the data does not already exist in table1 but am getting a unique key constraint error (cannot insert duplicate key). My code is as follows:
Code:
INSERT INTO table 1
SELECT table2.code,
table2.description
FROM table2
WHERE NOT EXISTS (
SELECT table1.code
FROM table1
WHERE table1.name = table2.name
Curiously, I can use the following, practically identical, code to copy the data one row at a time with no error:
Code:
INSERT INTO table 1
SELECT TOP 1
table2.code,
table2.description
FROM table2
WHERE NOT EXISTS (
SELECT table1.code
FROM table1
WHERE table1.name = table2.name
Any ideas?
DT