多表插入从另一个表中获取值

时间:2014-01-30 09:42:09

标签: oracle11g multi-table

我有桌子 - FAKE_CUST PRE_CUST NORMAL_CUST 。根据 FAKE_CUST 表中的约束条件,该值必须为 PRE_CUST NORMAL_CUST

我正在使用以下代码:

INSERT ALL
         INTO PRE_CUST(CUST_ID, TOTAL_COST_TRANS) (SELECT DISTINCT CUST_ID,C FROM (
                    SELECT CUST_ID, SUM(COST_TRANS) OVER (PARTITION BY CUST_ID) as C FROM FAKE_CUST) WHERE C>1000)

         INTO NORMAL_CUST(CUST_ID, TOTAL_COST_TRANS) (SELECT DISTINCT CUST_ID,C FROM (
                    SELECT CUST_ID, SUM(COST_TRANS) OVER (PARTITION BY CUST_ID) as C FROM FAKE_CUST) WHERE C<1000)
SELECT 1 FROM DUAL;

1 个答案:

答案 0 :(得分:0)

您开始使用多表插入时使用的语法不正确。

评估insert条件的WHEN子句必须在insert_into子句之前。

您的查询应如下所示:

INSERT ALL
   WHEN c > 1000 THEN
      INTO pre_cust (cust_id, total_cost_trans)
   WHEN c < 1000 THEN
      INTO normal_cust (cust_id, total_cost_trans)
   SELECT CUST_ID, SUM(COST_TRANS) OVER (PARTITION BY CUST_ID) as C 
   FROM   FAKE_CUST