Merge compare columns when null
Key words: merge compare columns
when we contact merge sql in ETL,
When we update some columns we should compare the value change or not.
We always write coalesce(columnname,valueifnull)<>coalesce(columnname,valueifnull)
But we should take care of the valueifnull, the value should not exist in tables and the data type
Should be right. Two points need to satisfy the requirements.
If you want to use sql to generate the sql automatically, you should know the column type for every column,
We can get this from database's meta data table, but we also need set the value if the column is null, this part
Will need a long case when , if not , we should have a configuration table to store this.
Another solution is :
Oracle:
Merge into target tar using (select * from source) as src
On tar.colx=src.colx…
When matched Update set col=src.col … where tar.col!=src.col or ( tar.col is null and src.col is not null) or (tar.col is not null and src.col is null)
It is long , if we execute it dynamically , the string may long exceed varchar2(4000);
Tar.col!=src.col (when the both columns are valid and not null)
Now, I think another expression to stand for not equal.
Not ( tar.col is null and src.col is null)
Sqlserver:
Merge into target tar using (select * from source) as src
On tar.colx=src.colx…
When matched and
(
tar.col<>src.col
Or not (tar.col is null and src.col is null)
Or ….
)
Update set coly=src.coly
…
You will notice that, there are some differences in merge implementation for oracle and sqlserver.
Slqserver is more powerful on this.
It support when matched and conditions but oracle not support this , but you can add the filter at where clause.
Just update what you want to update.
Another thing is sqlserver can judge the not matched case is caused by target table or source table.
When not matched by target table, insert data from source table.
When not matched by source talbe, just delete or do other actions.
On clause in merge is also need take care.
On tar.col=src.col and (tar.col is null and src.col is null)
Maybe is useful for your query.
Merge compare columns when null的更多相关文章
- Collectors.toMap不允许Null Value导致NPE
背景 线上某任务出现报警,报错日志如下: java.lang.NullPointerException: null at java.util.HashMap.merge(HashMap.java:12 ...
- Git 集成 Araxis Merge 作为比较和合并GUI工具的配置 参考自https://www.kancloud.cn/leviio/git/369125
Git 集成 Araxis Merge Win10下修改git全部配置文件方法Git 集成 Araxis Merge 作为比较和合并GUI工具的配置 那global对应的 ,gitconfig文件在哪 ...
- SQL基础笔记
Codecademy中Learn SQL, SQL: Table Transformaton和SQL: Analyzing Business Metrics三门课程的笔记,以及补充的附加笔记. Cod ...
- mysql的TABLE_SCHEMA的sql和information_schema表, MySQL管理一些基础SQL语句, Changes in MySQL 5.7.2
3.查看库表的最后mysql修改时间, 如果第一次新建的表可能还没有update_time,所以这里用了ifnull,当update_time为null时用create_time替代 select T ...
- Known BREAKING CHANGES from NH3.3.3.GA to 4.0.0
Build 4.0.0.Alpha1 ============================= ** Known BREAKING CHANGES from NH3.3.3.GA to 4.0. ...
- .NET导入导出Excel方法总结
最近,应项目的需求,需要实现Excel的导入导出功能,对于Web架构的Excel导入导出功能,比较传统的实现方式是: 1)导入Excel:将Excel文件上传到服务器的某一文件夹下,然后在服务端完成E ...
- PostgreSQL index types and index bloating
warehouse_db=# create table item (item_id integer not null,item_name text,item_price numeric,item_da ...
- yii 笔记
Yii1.1: $_GET 可以表示为 Yii::app()->request->getQuery() $_POST 可以表示为 Yii::app()->request->po ...
- Mapper XML Files详解
扫扫关注"茶爸爸"微信公众号 坚持最初的执着,从不曾有半点懈怠,为优秀而努力,为证明自己而活. Mapper XML Files The true power of MyBatis ...
随机推荐
- SQL compute by 的使用
SQL compute by 的使用 摘自:http://www.cnblogs.com/Gavinzhao/archive/2010/07/12/1776107.html GROUP BY子句有个缺 ...
- C#中部分方法返回值类型为什么只能是void?
这个问题答案选至<C#入门经典> 如果方法具有返回类型,那就可以作为表达式的一部分: x=Manipulate(y,z); 如果没有给部分方法提供实现代码,编译器就会在使用该方法的所有地方 ...
- 浅谈ImageList
ImageList组件用了很久,但是一直不太清楚它的实现原理,今天专门特意花了时间倒腾了下,终于弄明白了!于是在这里和大家分享下! 在设计页面中打卡工具箱-组件 找到ImageList组件,将它直接拖 ...
- String系列
String 简介 String 是java中的字符串,它继承于CharSequence.String类所包含的API接口非常多.为了便于今后的使用,我对String的API进行了分类,并都给出的演示 ...
- 编译安装memcached扩展记要
编译memcached扩展的时候,得指定libmemcached库的位置 --with-libmemcached-dir=DIR 来指定路径.这个路径就是安装libmemcached时指定的prefi ...
- ASP.NET WebAPI 11 参数验证
在绑定完Action的所有参数后,WebAPI并不会马上执行该方法,而要对参数进行验证,以保证输入的合法性. ModelState 在ApiController中一个ModelState属性用来获取参 ...
- HttpController的激活
Web API调用请求的目标是定义在某个HttpController类型中的某个Action方法,所以消息处理管道的最终需要激活目标HttpController对象.调用请求的URI会携带目标Http ...
- [Tool] 透过PowerPoint Online在部落格文章里内嵌简报
[Tool] 透过PowerPoint Online在部落格文章里内嵌简报 前言 讲课的时候,用PowerPoint做简报,好像已经成了讲课的惯例.而在课后,将课堂简报整理成部落格的文章,如果单纯是在 ...
- Java学习笔记之_JDBC
JDBC简介 1.SUN公司为了简化,统一数据库的操作,定义了一套Java操作数据库的规范,称之为JDBC 2.数据库驱动 3.JDBC全称为:JAVA DataBase Commectivity(j ...
- JS 面向对象 编程设计
面向对象的语言有一个标志,即拥有类的概念,抽象实例对象的公共属性与方法,基于类可以创建任意多个实例对象,一般具有封装.继承.多态的特性!但JS中对象与纯面向对象语言中的对象是不同的,ECMA标准定义J ...