I am inserting data into a table by selecting from another table which may have duplicates. I thought my query was handling this by checking if the row already exists but I am getting a unique constraint violation.
Here is the query:
INSERT INTO FOLDER_USER (FOLDER_ID, USER_ID)
SELECT DECODE(FOLDERID,'F10', '1','F565','2','F11', '3','F81', '4','0'), USERID
FROM DATA1.FOLDERS F1
WHERE UPPER(OWNER) = 'ADMIN'
AND NOT EXISTS
(SELECT 1 FROM FOLDER_USER F2
WHERE DECODE(FOLDERID,'F10', '1','F565','2','F11', '3','F81', '4','0')= F2.FOLDER_ID
AND F1.USERID = F2.USER_ID);
- Table FOLDER_USER contains 2 columns FOLDER_ID (number), USER_ID
(varchar) and combined they make up the primary key - Table FOLDERS contains 2 columns FOLDERID and USERID (both varchars). The value
in FOLDERID needs to be decoded into a number depending on its value
before being inserted into the new table
Perhaps the rows you are selecting are not unique within themselves.
Try: