oracle--insert
常规insert语法就不说了,还有些特殊用法
1. insert all
into table1(col1,col2) values(v1,v2)
into table2(col1,col2) values(v1,v2)
......
select ...;
后面的select直接影响前面的into,查询出的每条记录分别执行一次into
示例:
insert all into t_banks values(id,name,al_name) select * from t_banks; --t_banks表中的数据会加倍,values是后面select出的列名
insert all into t_banks values('1','1','1') select 1 from dual; --t_banks表插入一条记录('1','1','1')
2. insert all/first
when ? then
into table1(col1,col2) values(v1,v2)
when ? then
into table2(col1,col2) values(v1,v2)
else
into table3(col1,col2) values(v1,v2)
select ...;
all有条件的插入,会依次判断每个符合的条件然后into
first有条件的插入,会找到第一个满足的条件然后into,后面的就不判断了
ps:业务中有这样的场景,如果有满足条件的记录就不插入,否则插入。
单线程下先select再insert是ok的,但是如果用户通过界面同时操作就有可能在数据库插入同一数据。
解决方法如下
- 在service层对方法synchronized,粗暴有效
如果项目需要集群就不好控制了
- 数据库层面,在表上加唯一约束,需要动表
场景:现有一张表存借款需求,每条需求有一个编号,如果有在处理中的需求,相同编号的借款需求不允许再次插入,否则可以再次把借款需求插入表,进行处理
解决:表上加一个字段SEQ,存同一编号的多条记录的序号1,2,3,4......,编号和SEQ添加唯一键
这个时候如果表中已有数据,使用下面的sql为已有数据添加SEQ
merge into t_loan_request lr
using(
select decode(rownum-min_sno,0,1,rownum+1-min_sno)sno,a.lr_id
from
(select lr_applyid,lr_id from t_loan_request order by lr_applyid,lr_id)a,
(select lr_applyid,min(rownum) min_sno
from(select lr_applyid,lr_id from t_loan_request order by lr_applyid,lr_id)r
group by lr_aplyid)b
where a.lr_applyid=b.lr_applyid
)req
on (lr.lr_id=req.lr_id)
when matched then
update set lr.seq=req.sno
插入数据sql如下:
insert into t_loan_request(lr_id,lr_applyid,lr_requeststatus,seq)
select seq_loan_request.nextval,'000123','0',(select count(1)+1 from t_loan_request where lr_applyid='000123')
from dual
where not exists(select lr_id from t_loan_request where lr_requeststatus!='3' and lr_applyid='000123')
上面的语句如果并发,lr_applyid+seq相同,数据库检查唯一性,最终导致只有一条记录入库成功
或者使用merge插入语句
merge into t_loan_request lr
using (select count(1) cnt from t_loan_request where lr_requeststatus!='3' and lr_appplyid='000123')req
on(req.cnt<1)
when matched then
insert (lr_id,lr_applyid,lr_requeststatus,seq)
values(seq_loan_request.nextval,'000123','0',(select count(1)+1 from t_loan_request where lr_applyid='000123'))
和上面的sql效果一样
- sql改造,网上看的,但觉得不可靠
INSERT INTO T_TABLE SELECT ?,?,?,?,? FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM T_TABLE T WHERE T.FIELD1 = ? AND T.FIELD2 = ?)
只是依靠这一条语句,如果并发,都符合where条件,同时入库插入成功
oracle--insert的更多相关文章
- Oracle INSERT ALL 语句介绍
描述 Oracle INSERT ALL 语句用来用一个 INSERT 语句添加多行.该行可以只使用一个SQL命令插入到一个表或多个表. 语法 Oracle INSERT ALL 语法如下: INSE ...
- oracle Insert 一次插入多条记录
oracle Insert 一次插入多条记录有两种方法: 1)Insert All Into table_name values ... insert all into table_name valu ...
- Oracle Insert 多行(转)
1.一般的insert 操作. 使用语法insert into table_name[(column[,column...])] values (value[,value…])的insert语句,每条 ...
- oracle insert &字符插入问题
例如执行一下语句: insert into NSRXT_SP (SP_ID, SP_TITLE, SP_DESC, SP_URL, SP_TYPE, SP_SUB_TYPE, ADD_TIME, CZ ...
- [Oracle] Insert All神奇
无条件插入 Oracle中间insert all它指的是相同的数据组成不同的表.如果有需求现在:该t插入数据表t1,t2,假设你不知道insert all.您可以使用insert插入2次要,例如,见下 ...
- C# Oracle insert 中文乱码
问题描述: 在PL SQL中insert 中文数据,显示不乱码,通过后台insert的中文数据,显示问号. 解决分三步: 1.Select userenv('language') from dual; ...
- oracle insert into 插入多组数据方法总结
网上好多oracle 的文章,多是以oracle开头,内容确实其他sql,一幅气死人不偿命的嘴脸着实让人难受. 今天就更新点oracle 使用insert into插入数据的方式: 1.oracle ...
- oracle insert、append、parallel、随后查询的redo与磁盘读写
SQL> set autotrace traceonly statistics; SQL> insert into big_table_dir_test1 select * from bi ...
- [Oracle] Insert All的妙用
无条件的插入 Oracle中的insert all是指把同一批数据插入到不同的表中,假如如今有个需求:把t表中的数据分别插入t1,t2,假设你不知道insert all,你可能会使用insert插入2 ...
- ORACLE INSERT ALL 用法
1INSERT ALL 1.1句法 multi_table_insert :: = conditional_insert_clause :: = 1.2multi_table_insert 在多表插入 ...
随机推荐
- ocp 1Z0-051 141-175题解析
141. View the Exhibitand examine the structure of CUSTOMERS and GRADES tables. You need to displayna ...
- poj 1915 http://poj.org/problem?id=1915
/**< */#include <stdio.h> #include <string.h> #include <stdlib.h> #include < ...
- [iOS微博项目 - 1.2] - 导航栏搜索框
A.导航栏搜索框 1.需求 在“发现”页面,在顶部导航栏NavigationBar上添加一个搜索框 左端带有“放大镜”图标 github: https://github.com/hellovoidwo ...
- [C语言 - 2] C语言变量
A.变量的作用域: 1.局部变量:在函数或者代码块内部定义的变量 作用域:从定义处到代码块结束 生命周期:从定义处分配控件,代码块结束后被回收 局部变量没有默认值,要自己初始化 2.全局变量:在函 ...
- CentOS 搭建LNMP服务器和LAMP服务器
CentOS 搭建LNMP服务器 方法一:yum安装 1.更新YUM源 wget http://www.atomicorp.com/installers/atomic #下载atomic自动更新Y ...
- KVM学习笔记
检查机器是否启用KVM lsmod |grep kvm 安装KVM yum install libvirt python-virtinst qemu-kvm virt-viewer bridge-ut ...
- easyUI之tree
转自:http://www.cnblogs.com/CoreCaiNiao/archive/2010/08/20/1804387.html http://blog.csdn.net/l27775918 ...
- Enterprise Library 服务问题
在使用Enterprise Library而没有注册服务的时候会出现这样的问题,"Editing Post "Failed to create instances of perfo ...
- Date String转换
这种转换要用到java.text.SimpleDateFormat类 字符串转换成日期类型: 方法1: 也是最简单的方法 Date date=new Date("2008-04-14&quo ...
- C#不安全代码
当一个代码块使用 unsafe 修饰符标记时,C# 允许在函数中使用指针变量.不安全代码或非托管代码是指使用了指针变量的代码块. 下面的实例说明了不安全代码中的指针的定义与调用: static uns ...