Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

inserting values using merge

927867May 25 2012 — edited Jun 1 2012
Hi everyone,

I need help with inserting values using merge.

* I need to check all the units in a parent category. For example, NF_ARTICLECATEGORYID = 7462 is a parent category.
* Im going to compare all the units in the parent category(7642) to the units in a subcategory (8053).
* If the units in parent category(7642) is not present in the subcategory(8053) then the units will be inserted in the same table.

table structure:
Table name : ARTICLECATEGORYACCESS
Fields: IP_ARTICLECATEGORYACCESSID
NF_ARTICLECATEGORYID
NF_UNITID
NF_USERID
N_VIEW
N_EDIT

Sample data:
CREATE TABLE articlecategoryaccess (
IP_ARTICLECATEGORYACCESSID NUMBER(5),
NF_ARTICLECATEGORYID NUMBER (10),
NF_UNITID NUMBER (10),
NF_USERID NUMBER (10)
N_VIEW INT,
N_EDIT INT);

INSERT INTO articlecategoryaccess VALUES (255583, 7642, 29727, NULL, 1 ,1);
INSERT INTO articlecategoryaccess VALUES (243977,7642,29728, NULL, 1 ,1);
INSERT INTO articlecategoryaccess VALUES (240770,7642,29843, NULL, 1 ,1);
INSERT INTO articlecategoryaccess VALUES (243413,7642,29844, NULL, 1 ,1);
INSERT INTO articlecategoryaccess VALUES (274828,7642,44849, NULL, 1 ,1);

INSERT INTO articlecategoryaccess VALUES (274828,8053,44849, NULL, 1 ,1);

Units ID 29727, 29728, 29843, 29844, 44849 has access to parent category 7642.
The units id 29727, 29728, 29843, 29844 dont have access to subcategory 8053.
29727, 29728, 29843, 29844 should be inserted in the same table and will have an access to 8053.


After they are inserted, it should look like this


IP_ARTICLECATEGORYACCESSID NF_ARTICLECATEGORYID NF_UNITID NF_USERID N_VIEW N_EDIT

255583 7642 29727 null 1 1
243977 7642 29728 null 1 1
240770 7642 29843 null 1 1
243413 7642 29844 null 1 1
274828 7642 44849 null 1 1

new value 8053 44849 null 1 1
new value 8053 29843 null 1 1
new value 8053 29844 null 1 1
new value 8053 29728 null 1 1
new value 8053 29727 null 1 1


NOTE: IP_ARTICLECATEGORYACCESSID is a sequence and it should be unique


DECLARE
BEGIN
MERGE INTO articlecategoryaccess b
USING (SELECT *
FROM articlecategoryaccess c
WHERE nf_articlecategoryid = 7642
MINUS
SELECT *
FROM articlecategoryaccess c
WHERE nf_articlecategoryid = 8053) e
ON (1 = 2)

WHEN NOT MATCHED THEN
INSERT (b.ip_articlecategoryaccessid, b.nf_articlecategoryid, b.nf_unitid, b.NF_USERID, b.N_VIEW, b.N_EDIT)
VALUES (articlecategoryaccessid_seq.nextval, 8053, e.nf_unitid, null, 1, 1);
END;


i got an error after running the script:
*Cause: An UPDATE or INSERT statement attempted to insert a duplicate key.
For Trusted Oracle configured in DBMS MAC mode, you may see
this message if a duplicate entry exists at a different level.
*Action: Either remove the unique restriction or do not insert the key.


why would it be duplicated? its a sequence and its unique.. I dont know, maybe there is something wrong my script..

Any help is appreciated..

Ed
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 29 2012
Added on May 25 2012
4 comments
59 views