The total amount of 抽取
Will delete all the data target table, then the data source system are all inserted into the target table.
Note: This method ensures the quality of the data, but for the large amount of data tables, the poor performance.
① backup table, backup table structure
--表结构和数据一起备份到目标表
CREATE TABLE EMP_BACK AS (SELECT * FROM EMP );
--只备份了表结构
CREATE TABLE EMP_BACK1 AS (SELECT * FROM EMP WHERE 1 = 2 );
② increase the inspection field in the target table to help us determine whether the data actually updated.
ALTER TABLE EMP_BACK1 ADD DATA_DATE DATE; --新增时间戳字段
ALTER TABLE EMP_BACK1 ADD mark NUMBER(10); --新增了序列字段
③ increase the master key in the target table, the data to ensure the uniqueness
ALTER TABLE EMP_BACK1 ADD CONSTRAINT PK_EMP_BACK1 PRIMARY KEY(EMPNO);
The data ④TRUNCATE TABLE EMP_BACK1 empty target table
Creating full amount of stored data synchronization process
① create full amount of synchronization stored procedure
CREATE OR REPLACE PROCEDURE SP_EMP_BACK1 --创建存储过程SP_EMP_BACK1
IS
V_MARK NUMBER(10);--声明变量
BEGIN
-- 初始化变量
V_MARK := seq_test2.nextval;--序列下一个值
-- 使用动态SQL 清空目标表,让存储过程支持重跑
-- 第一种 删除效率比较高,但是必须使用动态SQL执行
/*EXECUTE IMMEDIATE 'TRUNCATE TABLE EMP_BACK1 ' ;*/
-- 第二种 直接使用delete 语句删除,记得加条件。
DELETE FROM EMP_BACK1 WHERE 1 = 1;
INSERT INTO EMP_BACK1(empno,
ename,
job,
mgr,
hiredate,
sal,
comm,
deptno,
DATA_DATE,
MARK)
SELECT
empno,
ename,
job,
mgr,
hiredate,
sal,
comm,
deptno,
SYSDATE,
V_MARK
FROM EMP_BACK ;
COMMIT; --提交
END ;
② call a stored procedure oh the whole amount of synchronous data
BEGIN
SP_EMP_BACK1;
END ;
③ query whether the data has been synchronized to the target table target table
SELECT * FROM EMP_BACK1;
Exercise
build a target table EMP_SALES (DEPTNO, DNAME, SAL_L1, SAL_L2, SAL_L3, mark) SAL_L1 ~ SAL_L3 corresponding rank 1-3 pay before
check field, require the use of a sequence implemented.
Write a stored procedure, use the full amount of synchronization, updating the target table EMP_SALES
① create the table structure
create table emp_sales(
DEPTNO number(7,2),
DNAME varchar2(20),
SAL_L1 number(7,2),
SAL_L2 number(7,2),
SAL_L3 number(7,2),
mark number(10)
);
② data source table
WITH E AS
(SELECT X.DEPTNO,X.DNAME,X.SAL,X.RN
FROM(
SELECT d.deptno,
d.dname,
e.sal,
dense_rank()OVER(PARTITION BY d.deptno ORDER BY e.sal DESC) RN
FROM emp e,dept d
WHERE e.deptno = d.deptno) X
WHERE X.RN <=3)
SELECT E.DEPTNO,
E.DNAME,
MAX(CASE RN WHEN 1 THEN E.SAL END) SAL_L1,
MAX(CASE RN WHEN 2 THEN E.SAL END) SAL_L2,
MAX(CASE RN WHEN 3 THEN E.SAL END) SAL_L3
FROM E
GROUP BY E.DEPTNO,E.DNAME;
③ create a sequence
CREATE SEQUENCE MYSEQ
MINVALUE 1
START WITH 1
NOMAXVALUE
INCREMENT BY 1
NOCYCLE
NOCACHE
④ create full amount of synchronization stored procedure
CREATE OR REPLACE PROCEDURE SP_QL_EMP_BACK
IS
V_MARK NUMBER(10);--声明变量
BEGIN
V_MARK := MYSEQ.NEXTVAL;
-- 第一种 删除效率比较高,但是必须使用动态SQL执行
--先清空目标表
EXECUTE IMMEDIATE 'TRUNCATE TABLE EMP_SALES ' ;
INSERT INTO EMP_SALES(DEPTNO,
DNAME,
SAL_L1,
SAL_L2,
SAL_L3,
MARK)
SELECT
DEPTNO,
DNAME,
SAL_L1,
SAL_L2,
SAL_L3,
V_MARK
FROM
(WITH E AS
(SELECT X.DEPTNO,X.DNAME,X.SAL,X.RN
FROM(
SELECT d.deptno,
d.dname,
e.sal,
dense_rank()OVER(PARTITION BY d.deptno ORDER BY e.sal DESC) RN
FROM emp e,dept d
WHERE e.deptno = d.deptno) X
WHERE X.RN <=3)
SELECT E.DEPTNO,
E.DNAME,
MAX(CASE RN WHEN 1 THEN E.SAL END) SAL_L1,
MAX(CASE RN WHEN 2 THEN E.SAL END) SAL_L2,
MAX(CASE RN WHEN 3 THEN E.SAL END) SAL_L3
FROM E
GROUP BY E.DEPTNO,E.DNAME
)
COMMIT;
END ;
⑤ call a stored procedure
BEGIN
SP_QL_EMP_BACK;
END;
⑥ query whether the data has been synchronized to the target table
SELECT * FROM EMP_SALES;