So say I have these two tables with the same columns. Use your imagination to make them bigger:
USER_COUNTERPARTY:
ID |Name |Credit Rating |Sovereign Risk |Invoicing Type
----+----------------------------+-----------------+------------------+---------------
1 |Nat Bank of Transnistria |7 |93 |Automatic
2 |Acme Ltd. |25 |12 |Automatic
3 |CowBInd LLP. |49 |12 |Manual
TEMP:
ID |Name |Credit Rating |Sovereign Risk |Invoicing Type
----+----------------------------+-----------------+------------------+---------------
2 |Acacacme Ltd. |31 |12 |Manual
4 |Disenthralled Nimrod Corp. |31 |52 |Automatic
and I want to merge them into one, replacing with the second one whatever has the same ID in the first one, and inserting whatever is not there. I can use this statement:
MERGE INTO USER_COUNTERPARTY C
USING TEMP T
ON (C.COUNTER_ID = T.COUNTER_ID)
WHEN MATCHED THEN UPDATE SET
C.COUNTER_NAME = T.COUNTER_NAME,
C.COUNTER_CREDIT = T.COUNTER_CREDIT,
C.COUNTER_SVRN_RISK = T.COUNTER_SVRN_RISK,
C.COUNTER_INVOICE_TYPE = T.COUNTER_INVOICE_TYPE
WHEN NOT MATCHED THEN INSERT VALUES (
T.COUNTER_ID,
T.COUNTER_NAME,
T.COUNTER_CREDIT,
T.COUNTER_SVRN_RISK,
T.COUNTER_INVOICE_TYPE);
Which is nice enough, but notice that I have to name each of the columns. Is there any way to merge these tables without having to name all the columns? Oracle documentation insists that I use column names after both 'insert' and 'set' in a merger, so some other statement might be needed. The result should be this:
ID |Name |Credit Rating |Sovereign Risk |Invoicing Type
----+----------------------------+-----------------+------------------+---------------
1 |Nat Bank of Transnistria |7 |93 |Automatic
2 |Acacacme Ltd. |31 |12 |Manual
3 |CowBInd LLP. |49 |12 |Manual
4 |Disenthralled Nimrod Corp. |31 |52 |Automatic
In case it helps I'm pasting this here:
CREATE TABLE USER_COUNTERPARTY
( COUNTER_ID INTEGER NOT NULL PRIMARY KEY,
COUNTER_NAME VARCHAR(38),
COUNTER_CREDIT INTEGER,
COUNTER_SVRN_RISK INTEGER,
COUNTER_INVOICE_TYPE VARCHAR(10) );
INSERT ALL
INTO USER_COUNTERPARTY VALUES (1, ‘Nat Bank of Transnistria’, 7, 93, ‘Automatic’)
INTO USER_COUNTERPARTY VALUES (2, ‘Acme Ltd.’, 25, 12, ‘Manual’)
INTO USER_COUNTERPARTY VALUES (3, ‘CowBInd LLP.’, 49, 12, ‘Manual’)
SELECT * FROM DUAL;
CREATE TABLE TEMP AS SELECT * FROM USER_COUNTERPARTY;
DELETE FROM TEMP;
INSERT ALL
INTO TEMP VALUES (2, ‘Conoco Ltd.’, 25, 12, ‘Automatic’)
INTO TEMP VALUES (4, ‘Disenthralled Nimrod Corp.’, 63, 12, ‘Manual’)
SELECT * FROM DUAL;