数据库中根据条件匹配如果有就更新没有就插入,多个sql不能一起执行解决方案

--在机构库表中根据机构代码查询如果可以查询到就把机构名称更新成最新的,如果查询不到就直接做插入操作。

begin

update test_institution set INSTITUTION_NAME='中石油资产有限公司' where institution_code ='T0001';

if sql%notfound then

insert into test_institution (OBJID, INSTITUTION_VIP, INSTITUTION_CODE, INSTITUTION_TYPE, INSTITUTION_STATUS, INSTITUTION_NAME, CREATETIMESTAMP, UPDATETIMESTAMP, VERSION) values ((select max(objid)+1 from test_institution), 1, 'T0001', '0', 0, '中石油资产有限公司', sysdate, sysdate, 0);

end if;

end;
/

特别注意:

如果是很多个sql要一起执行中间要用/分割开来,例如下面的:

begin

update test_institution set INSTITUTION_NAME='中石油资产有限公司' where institution_code ='T0001';

if sql%notfound then

insert into test_institution (OBJID, INSTITUTION_VIP, INSTITUTION_CODE, INSTITUTION_TYPE, INSTITUTION_STATUS, INSTITUTION_NAME, CREATETIMESTAMP, UPDATETIMESTAMP, VERSION) values ((select max(objid)+1 from test_institution), 1, 'T0001', '0', 0, '中石油资产有限公司', sysdate, sysdate, 0);

end if;

end;
/

begin

update test_institution set INSTITUTION_NAME='中石油资产有限公司2' where institution_code ='T0002';

if sql%notfound then

insert into test_institution (OBJID, INSTITUTION_VIP, INSTITUTION_CODE, INSTITUTION_TYPE, INSTITUTION_STATUS, INSTITUTION_NAME, CREATETIMESTAMP, UPDATETIMESTAMP, VERSION) values ((select max(objid)+1 from test_institution), 1, 'T0002', '0', 0, '中石油资产有限公司2', sysdate, sysdate, 0);

end if;

end;
/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值