Oracle有条件地插入数据

php中文网
发布: 2016-06-07 15:38:38
原创
2444人浏览过

方法一: declare iExists int; begin select count(*) into iExists from表 where 条件; if iExists=0 then insert into 表 ( ... ) values ( ... ); end if; end; 声明iExists变量,通过条件在表中找出有重复的数量,如果没有,就把数据插入表中 方法二:

方法一:

declare
iExists int;
begin
  select count(*) into iExists from 表 where 条件;
  if iExists=0 then
    insert into 表 ( ... ) values ( ... );
    end if;
  end;

声明iExists变量,通过条件在表中找出有重复的数量,如果没有,就把数据插入表中

 

方法二:

merge into 目标表
using 源表 | (select 语句)
on ( 条件 )
when matched then update set 列=值
    delete where ( 列=值 )
when not matched then insert ( 列... ) values ( 值... );

利用merge 方法,匹配的更新或删除,不匹配的插入数据

 

oracle不支持insert into ... not exists 方法

---------------------------------------------------------------------

手机在线人工冲值
手机在线人工冲值

说明:我不知道这个系统还能用到什么地方!他的运作方式是这样的,客户在其他地方比如掏宝购买了 你得卡,然后在你的网站进行冲值,你得有人登陆并看着后台,如果有人冲值,就会刷出记录,手工冲值完毕后,你得点击 [冲值完毕],客户的页面 就会返回 冲值信息!安装:上传所有文件,倒入(sql.txt)mysql数据库,使用myphpadminphplib 777phplib/sys.php 777phplib

手机在线人工冲值 0
查看详情 手机在线人工冲值

MSSQLServer 方法:

if not exists(select * from 表 where 条件... ) insert into 表 ( 列 ... ) values ( 值 ... );

或者

declare @iExists int
select @iExists=COUNT(*) from 表 where 条件... ;
if @iExists=0
begin
insert into 表 ( 列 ...) values ( 值 ... );
end;

----------------------------------------------------------------------------

注:插入的先后不同,会影响结果 (Oracle 与MSSQL不一样)

1、
merge into student
using (select * from dual)
on (student.stno=1 and (student.stname='aaaa' or student.stname is null))
when not matched then
  insert (stno,stname,birth )
  values (1,'aaaa',sysdate);

2、
merge into student
using (select * from dual)
on (student.stno=1 and (student.stname='' or student.stname is null))
when not matched then
  insert (stno,stname,birth )
  values (1,'',sysdate);

 

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号