多表无条件从子查询混淆中获取行

时间:2015-01-09 16:50:10

标签: sql oracle oracle11g

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_historymgr_history中选择哪些列进行插入?我可以看到sal_history中有3列,mgr_history中有3列,但在子查询中有4列。

我知道这个语句是如何工作的,对于子查询中的每一行," insert_into_clause"只执行一次,但这不是重点,只是这个小小的东西,我不了解列。

1 个答案:

答案 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_EMPIDSUB_SAL(带有希望澄清的前缀)出现在两者中。