博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SqlServer/Oracle 通过一个sql判断新增/修改
阅读量:6820 次
发布时间:2019-06-26

本文共 2437 字,大约阅读时间需要 8 分钟。

if (Config.DbInfo.DbType.Equals(DBType.SQLServer)){    sql = " IF EXISTS (SELECT 1 FROM wifi.imsi_model_status WHERE  device_id = @device_id and wireless='" + row[0].GetString() + "') UPDATE wifi.imsi_model_status SET model_status = @model_status,version=@version,serialnumber=@serialnumber, Log_Time=" + SysConst.time_sqlserver + " WHERE device_id = @device_id and wireless='" + row[0].GetString() + "' ELSE INSERT INTO wifi.imsi_model_status(Log_Time, device_id, wireless,model_status,version,serialnumber) VALUES(" + SysConst.time_sqlserver + ", @device_id,'" + row[0].GetString() + "', @model_status,@version,@serialnumber)";    cmdParms = new IDataParameter[4];    cmdParms[0] = new SqlParameter("@device_id", deviceId);    cmdParms[1] = new SqlParameter("@model_status", row[1].GetString());    cmdParms[2] = new SqlParameter("@version", ver);    cmdParms[3] = new SqlParameter("@serialnumber", ser);    sqlDic.Add(sql, cmdParms);}else if (Config.DbInfo.DbType.Equals(DBType.Oracle)){    sql = "MERGE INTO wifi.imsi_model_status a USING (select count(*) co from wifi.imsi_model_status WHERE  device_id = :device_id and wireless='" + row[0].GetString() + "') b  ON (b.co<>0) WHEN MATCHED THEN UPDATE  SET Log_Time=" + SysConst.time_sql + ",model_status = :model_status,version=:version,serialnumber=:serialnumber WHERE  device_id = :device_id and wireless='" + row[0].GetString() + "' WHEN NOT MATCHED THEN INSERT (Log_Time, device_id,wireless, model_status,version,serialnumber) VALUES(" + SysConst.time_sql + ", :device_id, '" + row[0].GetString() + "',:model_status,:version,:serialnumber)";    cmdParms = new IDataParameter[4];    cmdParms[0] = new OracleParameter(":device_id", deviceId);    cmdParms[1] = new OracleParameter(":model_status", row[1].GetString());    cmdParms[2] = new OracleParameter(":version", ver);    cmdParms[3] = new OracleParameter(":serialnumber", ser);    sqlDic.Add(sql, cmdParms);}

代码很简单:

1.sqlserver就是使用的exists关键字判断

2.oracle这边就是使用的merge关键字判断

 

sqlserver核心代码: 

if exists (select 1 from Teacher where id='1')    update Teacher set name='lisi',addr='重庆' where Id='1'else    insert into Teacher(id,name,addr) values(1,'lisi','重庆')

 

 Oracle核心代码: 

MERGE INTO Teacher USING (select count(1) co from Teacher WHERE id='1') on(co<>0)WHEN MATCHED THEN  update set name='lisi',addr='重庆' where Id='1'WHEN NOT MATCHED THEN  insert (id,name,addr) values(1,'lisi','重庆')

 

转载于:https://www.cnblogs.com/duanjt/p/10812117.html

你可能感兴趣的文章
伍雨霏-懂游戏的云服务如何保驾护航
查看>>
姜正林-CIO职业规划点滴感受
查看>>
win8下获取注册表权限
查看>>
js笔试题2
查看>>
Custom TabBarController
查看>>
用Myeclipse创建PhoneGap应用程序
查看>>
开源 java CMS - FreeCMS2.8 站内信
查看>>
kubeadm初始化kubernetes cluster的一点经验
查看>>
ZooKeeper应用案例
查看>>
springboot(二):thymeleaf模板开发
查看>>
高通camera架构
查看>>
php 使用DOMDocument 解析xml
查看>>
如何7步实现根据源码包创建rpm包
查看>>
hadoop2.0集群搭建详解
查看>>
Spring Cloud Alibaba基础教程:Nacos配置的多环境管理
查看>>
极乐小程序榜单(第六期)
查看>>
使用Log4j为项目配置日志输出应用详细总结及示例演示.
查看>>
Lua-5.3.2 安装 luasocket 的正确姿势
查看>>
freeswitch实战经验1:服务器向成员主动发起会议邀请
查看>>
python转换文本编码和windows换行符
查看>>