Home » SQL & PL/SQL » SQL & PL/SQL » Merge into table name using cursor (Oracle 10G Windows XP)
Merge into table name using cursor [message #462885] |
Mon, 28 June 2010 08:51  |
amul
Messages: 252 Registered: April 2001 Location: Chennai
|
Senior Member |
|
|
Hi,
I am trying to merge into test table using a cursor.
If the contract number in the test table matches with the contract number from the cursor then i need to update the creation_date else i need to insert values from the cursor into the test table. I know this is wrong usage of cursor, but i would appreciate suggestions regarding this.
FOR i IN cursortest
MERGE INTO test
USING cursortest
ON (i.contract_number = test.contract_number)
WHEN MATCHED THEN
UPDATE
SET creation_date = SYSDATE
WHEN NOT MATCHED THEN
INSERT (customer_id, contract_number, creation_date)
VALUES (i.customer_cpr_id, i.contract_number,SYSDATE);
end loop;
|
|
|
|
|
Re: Merge into table name using cursor [message #462889 is a reply to message #462887] |
Mon, 28 June 2010 09:15   |
ThomasG
Messages: 3212 Registered: April 2005 Location: Heilbronn, Germany
|
Senior Member |
|
|
In this cases (if contract_number is a primary key or a unique index) I often use the "try to insert, if it runs on a dup_val_on_index do an update" approach.
SQL> CREATE TABLE test_1 (
2 colA NUMBER(10),
3 colB NUMBER(10)
4 );
Table created.
SQL>
SQL> ALTER TABLE test_1 ADD CONSTRAINT pk_test_1 PRIMARY KEY(colA);
Table altered.
SQL>
SQL> CREATE TABLE test_2 (
2 colA NUMBER(10),
3 colB NUMBER(10)
4 );
Table created.
SQL>
SQL> INSERT INTO test_1 VALUES (1,1);
1 row created.
SQL> INSERT INTO test_1 VALUES (2,1);
1 row created.
SQL> INSERT INTO test_1 VALUES (3,1);
1 row created.
SQL> INSERT INTO test_1 VALUES (4,1);
1 row created.
SQL> INSERT INTO test_1 VALUES (5,1);
1 row created.
SQL>
SQL> SELECT * FROM test_1;
COLA COLB
---------- ----------
1 1
2 1
3 1
4 1
5 1
SQL>
SQL>
SQL> INSERT INTO test_2 VALUES (1,1);
1 row created.
SQL> INSERT INTO test_2 VALUES (2,2);
1 row created.
SQL> INSERT INTO test_2 VALUES (3,3);
1 row created.
SQL> INSERT INTO test_2 VALUES (4,4);
1 row created.
SQL> INSERT INTO test_2 VALUES (5,5);
1 row created.
SQL> INSERT INTO test_2 VALUES (6,6);
1 row created.
SQL> INSERT INTO test_2 VALUES (7,7);
1 row created.
SQL>
SQL> SELECT * FROM test_2;
COLA COLB
---------- ----------
1 1
2 2
3 3
4 4
5 5
6 6
7 7
7 rows selected.
SQL>
SQL> DECLARE
2 BEGIN
3 FOR l2 IN (SELECT * FROM test_2) LOOP
4 BEGIN
5 INSERT INTO test_1 VALUES l2;
6
7 EXCEPTION
8 WHEN Dup_Val_On_Index THEN
9 UPDATE test_1
10 SET ROW = l2
11 WHERE test_1.colA = l2.colA;
12 END;
13 END LOOP;
14 END;
15 /
PL/SQL procedure successfully completed.
SQL>
SQL> SELECT * FROM test_1;
COLA COLB
---------- ----------
1 1
2 2
3 3
4 4
5 5
6 6
7 7
7 rows selected.
SQL>
It might perhaps not be as fast as the merge, but it is definetely 'saver' when each row is for example a sale or a contract or another document where you want the other insert/update of the other documents to succeed even if one document has problems.
|
|
|
|
|
|
|
|
|
Goto Forum:
Current Time: Thu Apr 10 16:25:10 CDT 2025
|