一條指令分條件向多個表插入資料

2021-08-08 19:18:45 字數 1047 閱讀 6195

一條指令分條件向多個表插入資料的方法

1 以emp表作為實驗,要求:

部門10的記錄插入 e1表,

部門20的記錄插入 e2表,

部門30的記錄插入 e3表,

部門40的記錄插入 e4表,

其中e1、e2、e3、e4表結構與emp表部分或者全部相同

語句:--表結構相同時

insert all

when deptno=10 then into e1

when deptno=20 then into e2

when deptno=30 then into e3

else into e4

select * from emp;

--表結構不同時

create table e2 as select empno from emp where 1=2;

create table e3 as select hiredate from emp where 1=2;

create table e4 as select sal from emp where 1=2;

create table e5 as select comm from emp where 1=2;

create table e6 as select mgr,job from emp where 1=2;

insert all

when deptno=10 then into e2(empno)values(empno)

when deptno=20 then into e3(hiredate)values(hiredate)

when deptno=30 then into e4(sal)values(sal)

when deptno=40 then into e5(comm)values(comm)

else into e6(mgr,job)values(mgr,job)

select * from emp;

注意:insert all 和insert first的區別

--------------靈活運用

ORACLE SQL一條語句同時插入多個表

今天在網上看到乙個貼子,用一條語句,將資料同時插入多個表中,覺得很新奇,就自己練了一下,將語句記錄下來 step1 建立三個表,用於實驗測試 create table t table object id number,object name varchar2 128 object type varc...

向Oracle 資料表中插入一條帶有日期型別的資料

有一張表 batch 批次表 表的字段如下 第一種情況 現在需要插入一條當前的系統時間 sql 如下 insert into batch batch id,cus id,batch number,createtime,note values seq batch.nextval,2,000001 to...

取最後一條插入的資料

a2 hl x 10 09 14 排序後取最後一條啊 a2 hl x 10 09 39 你的id是遞增的還是隨機 a1 10 10 08 遞增a2 hl x 10 09 14 排序後取最後一條啊 a2 hl x 10 10 30 那就select max id from table a3 xx 10...