Oracle实现数据不存在则插入,数据存在则更新(insert or update)
思路是写一个函数,先按条件查询数据,假设查询到数据则更新。假设没有查询到数据则插入:
create or replace function fn_merge_index(statdate in date,
cpid in varchar2,
indextypecode in number,
indexitemcode in number,
indexdata in varchar2)
return number is
numb number;
begin
select count(*)
into numb
from cp_index_statistics_rec
where stat_date = to_date(to_char(statdate, 'yyyy/mm/dd'), 'yyyy/mm/dd')
and cp_id = cpid
and index_type_code = indextypecode
and index_item_code = indexitemcode;
if numb = 0 then
--数据不存在,insert
begin
insert into cp_index_statistics_rec
(stat_id,
stat_date,
diagnosis,
cp_id,
is_validate,
index_type_code,
index_item_code,
stat_data,
stat_create_date,
cp_name)
values
(cp_index_statistics_rec_seq.nextval,
to_date(to_char(statdate, 'yyyy/mm/dd'), 'yyyy/mm/dd'),
'',
cpid,
1,
indextypecode,
indexitemcode,
indexdata,
(select sysdate from dual),
(select cp_name from cp_templet_master where cp_id = cpid));
commit;
end;
else
--数据存在,update
begin
update cp_index_statistics_rec
set is_validate = 1,
stat_data = indexdata,
stat_create_date =
(select sysdate from dual)
where stat_date = to_date(to_char(statdate, 'yyyy/mm/dd'), 'yyyy/mm/dd')
and cp_id = cpid
and index_type_code = indextypecode
and index_item_code = indexitemcode;
commit;
end;
end if;
return numb;
end fn_merge_index;
注意to_date(to_char(statdate, 'yyyy/mm/dd'), 'yyyy/mm/dd')这个写法,假设写成to_date(statdate, 'yyyy/mm/dd'),依据NLS不同。可能导致数据出错。详细请看这里
另外oracle提供了merge into能够实现此功能。理论上讲比上面的效率会高。可是没做试验。merge into有个缺点就是在10g下面版本号的oracle中会出现故障,导致比較严重的后果(据说会把全部的数据都更新,而9i又不支持在update后加条件),所以我没有採用这种方法。
merge into的使用方法:
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
when not matched then insert (d.employee_id, d.bonus)
values (s.employee_id, s.salary*0.01);
另外还有个思路。直接update,运行后会返回受影响的行数。假设行数为0,表示没有符合条件的数据。后面运行insert;假设行数大于0。表示有符合条件的行数且update运行成功。
Oracle实现数据不存在则插入,数据存在则更新(insert or update)的更多相关文章
- 使用SQLServer2005插入一条数据时返回当前插入数据的ID
使用SQLServer2005插入一条数据时返回当前插入数据的ID 在执行完插入后 再执行 select @@identity from users 就OK 就是刚才插入的那行的 ID了 补充: @@ ...
- 45. 腾讯面试题: 使用hashmap 插入数据,怎么样依照插入数据的顺序输出数据
题目:使用hashmap 插入数据,怎么样依照插入数据的顺序输出数据 分析: 使用hashmap插入数据,数据的顺序会改变.能够写个小程序试试. 那怎么样依照插入的顺序输出呢? 方法一: 这是我第一时 ...
- Oracle使用语句块之循环插入数据
1.业务要求: 将oracle表A的整表的数据一次性导入到表B中 , 以A_ID为外键关联. (*******如果开发环境和实际生产环境的数据一致,而且数据量比较小情况,可以直接手动添加数据; ** ...
- MySQL 插入数据 通过命令提示窗口插入数据
MySQL 表中使用 INSERT INTO SQL语句来插入数据. 你可以通过 mysql> 命令提示窗口中向数据表中插入数据,或者通过PHP脚本来插入数据. 语法 以下为向MySQL数据表插 ...
- 在MyBatis中查询数据、涉及多参数的数据访问操作、插入数据时获取数据自增长的id、关联表查询操作、动态SQL、关于配置MyBatis映射没有代码提示的解决方案
1. 单元测试 在单元测试中,每个测试方法都需要执行相同的前置代码和后置代码,则可以自定义2个方法,分别在这2个方法中执行前置代码和后置代码,并为这2个方法添加@Before和@After注解,然后, ...
- MySQL 使用while语句向数据表中批量插入数据
1.创建一张数据表 mysql> create table test_while ( -> id int primary key) charset = utf8; Query OK, ro ...
- oracle数据库高级应用之《自动生成指定表的insert,update,delete语句》
/* * 多条记录连接成一条 * tableName 表名 * type 类型:可以是insert/update/select之一 */ create or replace function my_c ...
- Oracle批量插入数据SQL语句太长出错:无效的主机/绑定变量名
Oracle数据库,用mybatic批量插入数据: <insert id="saveBatch" parameterType="io.renren.entity.N ...
- oracle存储过程含参数的插入数据
create or replace procedure proczipcodebyzipinsert( i_zipcode in zipcode.zip%type, i_city in z ...
- mysql插入数据后返回自增ID的方法
mysql和oracle插入的时候有一个很大的区别是,oracle支持序列做id,mysql本身有一个列可以做自增长字段,mysql在插入一条数据后,如何能获得到这个自增id的值呢? 方法一是使用la ...
随机推荐
- delete 用法总结
// delete 删除 // 语法: boolean delete 数据; // 在当前作用域上删除数据 // 用法: // 1, 删除数组中的一个元素 // 2, 删除一个对象的属性或方法 // ...
- Gym - 100625J Jailbreak 最短路+搜索
http://codeforces.com/gym/100625/attachments/download/3213/2013-benelux-algorithm-programming-contes ...
- XDoclet学习
XDoclet可以通过你在java源代码中的一些特殊的注释信息,自动为你生成配置文件.源代码等等,例如web.ejb的部署描述文件.为你生成struts的struts-config.xml配置文件.j ...
- 【Docker自定制镜像之Dockerfile】
镜像的定制,就是定制每一层所添加的配置.文件,如果可以把每一层修改.安装.构建.操作的命令都写入到一个脚本中,用脚本来构建.定制镜像,这个脚本就是Dockerfile Dockerfile是一个文本文 ...
- mysql not in用法
select * from zan where uid not in(select uid from zan where zhongjiang !=0) group by uid order by r ...
- github连接报"ssh: connect to host github.com port 22: Connection timed out"错误
1. 异常 在连接github时,执行"ssh -T git@github.com" 命令时,出现 ssh: connect to host github.com port 22: ...
- HypericHQ
https://sourceforge.net/projects/hyperichq-zh-cn/?source=typ_redirect
- 洛谷—— P2580 于是他错误的点名开始了
https://www.luogu.org/problem/show?pid=2580 题目背景 XS中学化学竞赛组教练是一个酷爱炉石的人. 他会一边搓炉石一边点名以至于有一天他连续点到了某个同学两次 ...
- js函数的属性和方法
js函数的属性和方法 前面的话 函数是javascript中特殊的对象,可以拥有属性和方法,就像普通的对象拥有属性和方法一样.甚至可以用Function()构造函数来创建新的函数对象.本文是深入理解j ...
- 42.cnpm不是内部命令的解决方案:配置环境变量
转自:https://blog.csdn.net/u014540814/article/details/78777961