Multitable Inserts using INSERT ALL

Multitable inserts allow a single INSERT INTO .. SELECT statement to conditionally, or non-conditionally, insert into multiple tables. This statement reduces table scans and PL/SQL code necessary for performing multiple conditional inserts compared to previous versions. It's main use is for the ETL process in data warehouses where it can be parallelized and/or convert non-relational data into a relational format.
--Unconditional Insert into all tables
INSERT ALL
INTO ap_cust VALUES (customer_id, program_id, delivered_date)
INTO ap_orders VALUES (order_date, program_id)
SELECT program_id, delivered_date, customer_id, order_dateFROM airplanes;
-- Pivoting insert to split non-relational data
INSERT ALL
INTO Sales_info VALUES (employee_id,week_id,sales_MON)
INTO Sales_info VALUES (employee_id,week_id,sales_TUE)
INTO Sales_info VALUES (employee_id,week_id,sales_WED)
INTO Sales_info VALUES (employee_id,week_id,sales_THUR)
INTO Sales_info VALUES (employee_id,week_id, sales_FRI)
SELECT EMPLOYEE_ID, week_id, sales_MON, sales_TUE,
sales_WED, sales_THUR,sales_FRI FROM Sales_source_data;
-- Conditionally insert into ALL tables
INSERT ALL
WHEN SAL>10000 THEN INTO sal_history VALUES(EMPID,HIREDATE,SAL)
WHEN MGR>200 THEN INTO mgr_history VALUES(EMPID,MGR,SYSDATE)
SELECT employee_id EMPID, hire_date HIREDATE, salary SAL, manager_id MGR
FROM employees WHERE employee_id > 200;

-- Insert into the FIRST table with a matching condition
INSERT FIRST
WHEN SAL > 25000 THEN INTO special_sal VALUES(DEPTID,SAL)
WHEN HIREDATE like ('%') THEN INTO hiredate_history_00 VALUES(DEPTID,HIREDATE)
WHEN HIREDATE like ('%99%') THEN INTO hiredate_history_99 VALUES(DEPTID,HIREDATE)
ELSE INTO hiredate_history VALUES(DEPTID, HIREDATE)
SELECT department_id DEPTID, SUM(salary) SAL, MAX(hire_date) HIREDATE
FROM employees GROUP BY department_id;

4 comments :

  1. I need help with my assignmenr.Well I want insert multiple values into my tables but i dont understand the select statement.
    the name of the table is SKILL and the attributes are SKILL_ID & SKILL_NAME.Any help wld be much appreciated.....
    ram20_rage@yahoo.com

    ReplyDelete
  2. insert all into tablename(column1,column2,column3)values(column1,clumn2,column3)select * from dual;

    sureshbabu. babuspeaking@gamil.com,babumailing@gamil.com

    ReplyDelete
  3. INSERT ALL
    WHEN A.EMP_ID BETWEEN B.EMP_FIRST AND B.EMP_FIRST +100
    INTO OLD_EMP (X,Y,Z) VALUES (A,B,C)
    WHEN A.EMP_ID BETWEEN B.EMP_FIRST AND B.EMP_FIRST +5
    INTO BOARD (L,M) VALUES (D,E)
    SELECT A,B,C,D,E FROM EMP A,EMPLIST B;

    Query throws an error that B.EMP_FIRST is an invalid identifier.
    Can I not use columns of other tables in WHEN clause of INSERT ALL Statement?

    ReplyDelete