PLSQL_基础系列07_插入方式Pivoting / Unconditional / Conditional ALL / Conditional FIRST INSERT(案例)

简介: 2014-12-08 Created By BaoXinjian 一、摘要 Oracle Insert的多种方式 1. standard insert 1. pivoting Insert 2.

2014-12-08 Created By BaoXinjian

一、摘要


Oracle Insert的多种方式

1. standard insert

1. pivoting Insert

2. unconditional insert

3. conditional all insert / conditional first insert

 

二、标准Insert - 单表单行插入


标准Insert -- 单表单行插入

1. 语法:

INSERT INTO table [(column1,column2,...)] VALUE (value1,value2,...)

2. 示例:

INSERT INTO dep (dep_id,dep_name) VALUES(1,'技术部');

3. 备注:

用标准语法只能插入一条数据,且只能在一张表中插入数据

 

三、无条件 Insert All - 多表多行插入


无条件 Insert all -- 多表多行插入

1. 语法:

INSERT [ALL] [condition_insert_clause]

[insert_into_clause values_clause] (subquery)

2. 示例:

INSERT ALL
  INTO sal_history(emp_id,hire_date,salary) values (empid,hiredate,sal)
  INTO mgr_history(emp_id,manager_id,salary) values (empid,hiredate,sal)
SELECT employee_id empid,hire_date hiredate,salary sal,manager_id mgr
  FROM employees
 WHERE employee_id>200;

 

四、有条件的Insert


有条件的Insert

1. 语法:

INSERT [ALL | FIRST]

WHEN condition THEN insert_into_clause values_clause

[WHEN condition THEN] [insert_into_clause values_clause]

......

[ELSE] [insert_into_clause values_clause]

Subquery;

2. 示例:

INSERT All
WHEN id>5 THEN INTO z_test1(id, name) VALUES(id,name)
WHEN id<>2 THEN INTO z_test2(id) VALUES(id)
ELSE INTO z_test3 VALUES (name)
SELECT id,name FROM z_test;

当使用ALL关键字时,oracle会从上至下判断每一个条件,当条件满足时就执行后面的into语句

在上面的例子中,如果ID=6 那么将会在z_test1中插入一条记录,同时也在z_test2中插入一条记录

3. 备注:

当使用FIRST关键字时,oracle会从上至下判断每一个条件,当遇到第一个满足时就执行后面的into语句,

同时中断判断的条件判断,在上面的例子中,如果ID=6,仅仅会在z_test1中插入一条数据

 

五、旋转Pivoting Insert


旋转Insert (pivoting insert)

create table sales_source_data (
employee_id number(6),
week_id number(2),
sales_mon number(8,2),
sales_tue number(8,2),
sales_wed number(8,2),
sales_thur number(8,2),
sales_fri number(8,2)
);

insert into sales_source_data values (176,6,2000,3000,4000,5000,6000);
create table sales_info (
employee_id number(6),
week number(2),
sales number(8,2)
);

 看上面的表结构,现在将要sales_source_data表中的数据转换到sales_info表中,这种情况就需要使用旋转Insert

2. 示例如下:

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;

 

3. 从该例子可以看出,所谓旋转Insert是无条件 insert all 的一种特殊应用,但这种应用被oracle官方,赋予了一个pivoting insert的名称,即旋转insert

 

Thanks and Regards

 

ERP技术讨论群: 288307890
技术交流,技术讨论,欢迎加入
Technology Blog Created By Oracle ERP - 鲍新建
相关文章
|
2月前
|
SQL Java 关系型数据库
MyBatis的动态SQL之OGNL(Object-Graph Navigation Language)表达式以及各种标签的用法
MyBatis的动态SQL之OGNL(Object-Graph Navigation Language)表达式以及各种标签的用法
18 0
|
12月前
|
SQL Java 数据库连接
MyBatis动态SQL中if、where、trim、choose、when、otherwise、foreach标签及sql标签范例
MyBatis动态SQL中if、where、trim、choose、when、otherwise、foreach标签及sql标签范例
99 0
SAP QM初阶执行事务代码QDB1,报错- Sampling procedure NM000001 has no sampling scheme-
SAP QM初阶执行事务代码QDB1,报错- Sampling procedure NM000001 has no sampling scheme-
SAP QM初阶执行事务代码QDB1,报错- Sampling procedure NM000001 has no sampling scheme-
SAP QM执行事务代码QE23为检验批录入结果,报错-No selected set exists for the inspection point 200 or plant NMDC-
SAP QM执行事务代码QE23为检验批录入结果,报错-No selected set exists for the inspection point 200 or plant NMDC-
SAP QM执行事务代码QE23为检验批录入结果,报错-No selected set exists for the inspection point 200 or plant NMDC-
SAP QM中阶执行事务代码QDB1,报错- Inspection severity 001 AQL 0.650 not in sampling schema A01-
SAP QM中阶执行事务代码QDB1,报错- Inspection severity 001 AQL 0.650 not in sampling schema A01-
SAP QM中阶执行事务代码QDB1,报错- Inspection severity 001 AQL 0.650 not in sampling schema A01-
ABAP check table的工作原理
Created by Jerry Wang on Sep 29, 2016
122 0
ABAP check table的工作原理
一个ABAP重构的实例:CL_CRM_LEAD_CREATE~SELECT_CAMPAIGNS_BY_SQL
Created by Wang, Jerry, last modified on Nov 12, 2015
116 0
|
SQL OLAP 数据库
CMU 15-721 15-查询执行和处理过程 Query Execution & Processing
数据库架构 从整体数据库架构来看,我们可以知道分为网络协议层、优化器层、执行器、存储,详见下图:SQL从客户端发送到数据库服务端,经过解析器、语法语义分析、逻辑优化和物理优化,从一个字符串转换为解析树、语法树到最终的执行计划,那么最终是如何变成机器可以执行的操作呢,本文重点就是来讲执行器的一些重要组件和原理。
1873 0
重构——38以卫语句取代嵌套条件表达式(Replace Nested Conditional with Guard Clause)
以卫语句取代嵌套条件表达式(Replace Nested Conditional with Guard Clause):函数中的条件逻辑使人难以看清正常的执行路径;使用卫语句表现所有特殊情况
3747 0