sql merge example01

1)sql merge example01
1@@@@loading the dimensions using sql merge
@@@
@@@<1>create customer table.
@@@
[oracle@station78 ~]$ cat create_customer.sql
CREATE TABLE  dw.customer
(CUSTOMER_ID VARCHAR2(10),
 GENDER  VARCHAR2(1),
 TAX_RATE NUMBER,
 CITY VARCHAR2(15),
 STATE    VARCHAR2(10),
 REGION VARCHAR2(15),
 POSTAL_CODE VARCHAR2(10),
 COUNTRY  VARCHAR2(20),
 OCCUPATION VARCHAR2(15)
)
tablespace users;

@@@
[oracle@station78 ~]$ cat insert_customer.sql
INSERT INTO dw.customer VALUES   
('AB123459', 'M',  5, 'Phoenix', 'AZ', 'AmerWest', '85001', 'USA', 'Astor-Physicist');
INSERT INTO dw.customer VALUES   
('AB123460', 'F', 15, 'London', 'London', 'EuroWest', 'W1-2BA', 'UK', 'Engineer');
COMMIT;

@@@
SYS@ocp> @create_customer.sql
Table created.
SYS@ocp> @insert_customer.sql
1 row created.
1 row created.
Commit complete.
SYS@ocp> select count(*) from dw.customer;
  COUNT(*)
----------
     2



@@@
@@@<2>create external customer change table to dw user.
@@@
[oracle@station78 datafiles]$ cat customer_changes.dat
'AB123459', 'M',  5, 'Phoenix', 'AZ', 'AmerWest', '85001', 'USA', 'Astor-Physicist'
'AB123460', 'F', 15, 'London', 'London', 'EuroWest', 'W1-2BA', 'UK', 'Engineer'
'AB114778', 'M', 40, 'Reading', 'Berkshire', 'EuroWest', 'RG1 1BB', 'UK', 'Astronomer'
'AB123478', 'F', 25, 'Camberley', 'Surrey', 'EuroWest', 'GU14 2DR', 'UK', 'DB Consultant'

@@@
[oracle@station78 ~]$ cat external_customer_changes.sql
CREATE TABLE dw.customer_changes
(customer_id   VARCHAR2(10),
 gender    VARCHAR2(1),
 tax_rate   NUMBER,
 city   VARCHAR2(15),
 state VARCHAR2(10),
 region  VARCHAR2(15),
 postal_code VARCHAR2(10),
 country   VARCHAR2(20),
 occupation  VARCHAR2(15))
ORGANIZATION EXTERNAL
(TYPE ORACLE_LOADER
   DEFAULT DIRECTORY data_file_dir
   ACCESS PARAMETERS
     (records delimited by newline
      characterset us7ascii
      badfile log_file_dir:'cust_changes.bad'
      logfile log_file_dir:'cust_changes.log'
      fields terminated by ','
      optionally enclosed by "'")
LOCATION ('customer_changes.dat')
)
REJECT LIMIT UNLIMITED NOPARALLEL;

@@@
SYS@ocp> @external_customer_changes.sql
Table created.

@@@
SYS@ocp> select count(*) from dw.customer_changes;
  COUNT(*)
----------
     4


@@@
@@@<3>merge customer_changes table and customer table.
@@@
@@@all the changed table of customer would apply into customer.
@@@this is a good method to synchronize the data between table in different databases.
[oracle@station78 ~]$ cat merge01.sql
MERGE INTO dw.customer c
USING dw.customer_changes cc
ON (c.customer_id=cc.customer_id)
WHEN MATCHED THEN
UPDATE SET
  c.city=cc.city,
  c.state=cc.state,
  c.postal_code=cc.postal_code,
  c.gender=cc.gender,
  c.country=cc.country,
  c.region=cc.region,
  c.tax_rate=cc.tax_rate,
  c.occupation=cc.occupation
WHEN NOT MATCHED THEN
INSERT
(customer_id, city, state, postal_code, gender, region,
 country, tax_rate, occupation)
VALUES
(cc.customer_id, cc.city, cc.state, cc.postal_code,
 cc.gender, cc.region, cc.country, cc.tax_rate,
 cc.occupation);

@@@
SYS@ocp> @merge01.sql
4 rows merged.
SYS@ocp> select count(*) from dw.customer;
  COUNT(*)
----------
     4

@@@
@@@supplement: if target table is read only, you could change code like:
 MERGE INTO dw.customer c
 USING dw.customer_changes cc
 ON (c.customer_id=cc.customer_id)
 WHEN MATCHED THEN
 WHEN NOT MATCHED THEN
 INSERT
 (customer_id, city, state, postal_code, gender, region,
  country, tax_rate, occupation)
 VALUES
 (cc.customer_id, cc.city, cc.state, cc.postal_code,
  cc.gender, cc.region, cc.country, cc.tax_rate,
  cc.occupation);


@@@
@@@<4>add the delete clause.
@@@
[oracle@station78 ~]$ cat merge02.sql
MERGE INTO dw.customer c
USING dw.customer_changes cc
ON (c.customer_id=cc.customer_id)
WHEN MATCHED THEN
UPDATE SET
  c.city=cc.city,
  c.state=cc.state,
  c.postal_code=cc.postal_code,
  c.gender=cc.gender,
  c.country=cc.country,
  c.region=cc.region,
  c.tax_rate=cc.tax_rate,
  c.occupation=cc.occupation
DELETE WHERE(c.tax_rate=5)    @@@<==it is work to customer
WHEN NOT MATCHED THEN
INSERT
(customer_id, city, state, postal_code, gender, region,
 country, tax_rate, occupation)
VALUES
(cc.customer_id, cc.city, cc.state, cc.postal_code,
 cc.gender, cc.region, cc.country, cc.tax_rate,
 cc.occupation);

@@@
SYS@ocp> @merge02.sql
4 rows merged.
SYS@ocp> select count(*) from dw.customer;
  COUNT(*)
----------
     3

你可能感兴趣的:(sql,merge,merge)