思路是写一个函数,先按条件查询数据,假设查询到数据则更新。假设没有查询到数据则插入:

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)的更多相关文章

  1. 使用SQLServer2005插入一条数据时返回当前插入数据的ID

    使用SQLServer2005插入一条数据时返回当前插入数据的ID 在执行完插入后 再执行 select @@identity from users 就OK 就是刚才插入的那行的 ID了 补充: @@ ...

  2. 45. 腾讯面试题: 使用hashmap 插入数据,怎么样依照插入数据的顺序输出数据

    题目:使用hashmap 插入数据,怎么样依照插入数据的顺序输出数据 分析: 使用hashmap插入数据,数据的顺序会改变.能够写个小程序试试. 那怎么样依照插入的顺序输出呢? 方法一: 这是我第一时 ...

  3. Oracle使用语句块之循环插入数据

    1.业务要求:  将oracle表A的整表的数据一次性导入到表B中 , 以A_ID为外键关联. (*******如果开发环境和实际生产环境的数据一致,而且数据量比较小情况,可以直接手动添加数据; ** ...

  4. MySQL 插入数据 通过命令提示窗口插入数据

    MySQL 表中使用 INSERT INTO SQL语句来插入数据. 你可以通过 mysql> 命令提示窗口中向数据表中插入数据,或者通过PHP脚本来插入数据. 语法 以下为向MySQL数据表插 ...

  5. 在MyBatis中查询数据、涉及多参数的数据访问操作、插入数据时获取数据自增长的id、关联表查询操作、动态SQL、关于配置MyBatis映射没有代码提示的解决方案

    1. 单元测试 在单元测试中,每个测试方法都需要执行相同的前置代码和后置代码,则可以自定义2个方法,分别在这2个方法中执行前置代码和后置代码,并为这2个方法添加@Before和@After注解,然后, ...

  6. MySQL 使用while语句向数据表中批量插入数据

    1.创建一张数据表 mysql> create table test_while ( -> id int primary key) charset = utf8; Query OK, ro ...

  7. oracle数据库高级应用之《自动生成指定表的insert,update,delete语句》

    /* * 多条记录连接成一条 * tableName 表名 * type 类型:可以是insert/update/select之一 */ create or replace function my_c ...

  8. Oracle批量插入数据SQL语句太长出错:无效的主机/绑定变量名

    Oracle数据库,用mybatic批量插入数据: <insert id="saveBatch" parameterType="io.renren.entity.N ...

  9. oracle存储过程含参数的插入数据

    create or replace procedure proczipcodebyzipinsert(   i_zipcode  in  zipcode.zip%type,   i_city in z ...

  10. mysql插入数据后返回自增ID的方法

    mysql和oracle插入的时候有一个很大的区别是,oracle支持序列做id,mysql本身有一个列可以做自增长字段,mysql在插入一条数据后,如何能获得到这个自增id的值呢? 方法一是使用la ...

随机推荐

  1. 计算文件大小(C/C++语言)

    #include <stdio.h> int main() { FILE* fp; if (fp = fopen("read files.exe", "r&q ...

  2. BZOJ 3196 线段树套平衡树

    (代码无比丑陋) //By SiriusRen #include <cstdio> #include <algorithm> using namespace std; int ...

  3. BZOJ 2730 矿场搭建 Tarjan求割点

    思路: Tarjan求出来点双&割点 判一判就行了 //By SiriusRen #include <stack> #include <cstdio> #include ...

  4. openSUSE leap 42.3 添加HP Laserjet Pro M128fn打印机和驱动

    一.安装驱动 YaST控制中心->软件管理->搜索->hplip 安装hplip 如下图: HPLIP(Linux Imaging and Printing Object)以前有hp ...

  5. IBM将收购Linux发行商红帽公司,继续发力云计算市场

    10月29日凌晨消息,IBM和Red Hat当地时间星期日联合宣布,IBM将以340亿美元收购红帽公司(Red Hat).根据两家公司发表的一份联合声明,IBM将以每股190美元的价格,以现金方式收购 ...

  6. rev---将文件中的每行内容以字符为单位反序输出

    rev命令将文件中的每行内容以字符为单位反序输出,即第一个字符最后输出,最后一个字符最先输出,依次类推.

  7. 强悍的 vim —— 删除空行、删除注释以及加注释解注释

    强悍的 vim -- 删除空行.删除注释以及加注释解注释 原文 https://blog.csdn.net/lanchunhui/article/details/51588198 1. 删除空行空行的 ...

  8. vim 计算器寄存器使用

    我们可能会在vim的使用中,碰到下面的情况 当我正在写一周预算的时候,我想计算下每天我买菜花2.7,每天买两顿,周死晚上出去吃,周六额外买1.5斤14.8一斤的猪肉... 这时候你打算怎么办呢,是不是 ...

  9. Resize图片

    在网站上传图片的时候,提示图片太大了. 有5种方式来调整图片大小 http://www.wikihow.com/Resize-a-JPEG picresize.com 这个网站比较靠谱:使用Windo ...

  10. 如何使iframe外部的超级链接的页面在iframe中打开

    如何使iframe外部的超级链接的页面在iframe中打开,有以下两种方法: 一.html方法: <iframe name="a1"></iframe> & ...