• SqlServer/Oracle 通过一个sql判断新增/修改


    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','重庆')  
  • 相关阅读:
    Django 用ModelForm批量保存form表单(非常实用的方法) mfor_verity项目
    jquery ajax异步提交表单数据的方法
    python字符串转换成变量的几种方法
    django 线上线下使用不同的数据库 上线:mysql 线下sqlite3 以及debug模式的开和关
    django admin 或xdmin list_display search_fields list_filter 如果显示搜索外键或多对多字段
    nonce和timestamp在Http安全协议中的作用
    Web API接口 安全验证
    .Net环境下的缓存技术介绍
    .Net缓存管理框架CacheManager
    在asp.net web api中利用过滤器设置输出缓存
  • 原文地址:https://www.cnblogs.com/duanjt/p/10812117.html
Copyright © 2020-2023  润新知