ITPub博客

首页 > 数据库 > Oracle > Oracle的INSERT ALL/FIRST和MERGE INTO

Oracle的INSERT ALL/FIRST和MERGE INTO

Oracle 作者:嘟嘟嗒嗒跳跳 时间:2010-01-29 16:04:42 0 删除 编辑

要拆分一个大表,想起来Oracle中有个向多表插入数据的insert语句,可惜teradata暂不支持。

记录一下格式及用法:
INSERT ALL/FIRST
   WHEN ottl < 100000 THEN
      INTO small_orders VALUES(oid, ottl, sid, cid)
   WHEN ottl > 100000 and ottl < 200000 THEN
      INTO medium_orders VALUES(oid, ottl, sid, cid)
   WHEN ottl > 200000 and ottl < 300000 THEN
      INTO large_orders VALUES(oid, ottl, sid, cid)
   ELSE
      INTO special_orders
   SELECT o.order_id oid, o.customer_id cid, o.order_total ottl, o.sales_rep_id sid,
             
c.credit_limit cl, c.cust_email cem
   FROM orders o, customers c
   WHERE o.customer_id = c.customer_id;

ALL与FIRST的区别在于:
If you specify ALL, the default value, then the database evaluates each WHEN clause regardless of the results of the evaluation of any other WHEN clause. For each WHEN clause whose condition evaluates to true, the database executes the corresponding INTO clause list.
If you specify FIRST, then the database evaluates each WHEN clause in the order in which it appears in the statement. For the first WHEN clause that evaluates to true, the database executes the corresponding INTO clause and skips subsequent WHEN clauses for the given row.

 

MERGE INTO bonuses D
   USING (SELECT employee_id, salary, department_id FROM employees
             WHERE department_id = 80) S
   ON (D.employee_id = S.employee_id)
   WHEN MATCHED THEN
       UPDATE SET D.bonus = D.bonus + S.salary*.01
       DELETE WHERE (S.salary > 8000)
   WHEN NOT MATCHED THEN
       INSERT (D.employee_id, D.bonus) VALUES (S.employee_id, S.salary*0.1)
       WHERE (S.salary <= 8000);

The merge_update_clause specifies the new column values of the target table. Oracle performs this update if the condition of the ON clause is true. If the update clause is executed, then all update triggers defined on the target table are activated.
Specify the where_clause if you want the database to execute the update operation only if the specified condition is true. The condition can refer to either the data source or the target table. If the condition is not true, then the database skips the update operation when merging the row into the table.
Specify the DELETEwhere_clause to clean up data in a table while populating or updating it. The only rows affected by this clause are those rows in the destination table that are updated by the merge operation. That is, the DELETEWHERE condition evaluates the updated value, not the original value that was evaluated by the UPDATESET ... WHERE condition. If a row of the destination table meets the DELETE condition but is not included in the join defined by the ON clause, then it is not deleted. Any delete triggers defined on the target table will be activated for each row deletion.

The merge_insert_clause specifies values to insert into the column of the target table if the condition of the ON clause is false. If the insert clause is executed, then all insert triggers defined on the target table are activated. If you omit the column list after the INSERT keyword, then the number of columns in the target table must match the number of values in the VALUES clause.
To insert all of the source rows into the table, you can use a constant filter predicate in the ON clause condition. An example of a constant filter predicate is ON (0=1). Oracle Database recognizes such a predicate and makes an unconditional insert of all source rows into the table. This approach is different from omitting the merge_update_clause. In that case, the database still must perform a join. With constant filter predicate, no join is performed.
Specify the where_clause if you want Oracle Database to execute the insert operation only if the specified condition is true. The condition can refer only to the data source table. Oracle Database skips the insert operation for all rows for which the condition is not true.

<!-- 正文结束 -->

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/21377477/viewspace-1117035/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 没有了~
下一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2009-04-13