INSERT ALL
INTO sal_history (empid,hiredate,sal)
VALUES (empid,hiredate,sal)
INTO mgr_history (empid,mgr,sal)
VALUES (empid,mgr,sal)
SELECT employee_id AS "EMPID",
hire_date AS "HIREDATE",
salary AS "SAL",
manager_id AS "MGR"
FROM employees
WHERE employee_id > 200;
insert into子句如何在整个语句中知道在多表sal_history
和mgr_history
中选择哪些列进行插入?我可以看到sal_history
中有3列,mgr_history
中有3列,但在子查询中有4列。
我知道这个语句是如何工作的,对于子查询中的每一行," insert_into_clause"只执行一次,但这不是重点,只是这个小小的东西,我不了解列。
答案 0 :(得分:3)
每个INTO
子句指定要为该表填充的列,匹配的VALUES
子句指定要使用的子查询中的哪些列。在这种情况下,子查询使用相同的名称,这可能有点令人困惑;也许你会发现不同的别名更清楚:
INSERT ALL
INTO sal_history (empid,hiredate,sal)
VALUES (sub_empid,sub_hiredate,sub_sal)
INTO mgr_history (empid,mgr,sal)
VALUES (sub_empid,sub_mgr,sub_sal)
SELECT employee_id AS "SUB_EMPID",
hire_date AS "SUB_HIREDATE",
salary AS "SUB_SAL",
manager_id AS "SUB_MGR"
FROM employees
WHERE employee_id > 200;
VALUES
子句中使用了所有四个子查询列(别名);每个都有三个,SUB_EMPID
和SUB_SAL
(带有希望澄清的前缀)出现在两者中。