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的更多相关文章

  1. Collectors.toMap不允许Null Value导致NPE

    背景 线上某任务出现报警,报错日志如下: java.lang.NullPointerException: null at java.util.HashMap.merge(HashMap.java:12 ...

  2. Git 集成 Araxis Merge 作为比较和合并GUI工具的配置 参考自https://www.kancloud.cn/leviio/git/369125

    Git 集成 Araxis Merge Win10下修改git全部配置文件方法Git 集成 Araxis Merge 作为比较和合并GUI工具的配置 那global对应的 ,gitconfig文件在哪 ...

  3. SQL基础笔记

    Codecademy中Learn SQL, SQL: Table Transformaton和SQL: Analyzing Business Metrics三门课程的笔记,以及补充的附加笔记. Cod ...

  4. 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 ...

  5. 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. ...

  6. .NET导入导出Excel方法总结

    最近,应项目的需求,需要实现Excel的导入导出功能,对于Web架构的Excel导入导出功能,比较传统的实现方式是: 1)导入Excel:将Excel文件上传到服务器的某一文件夹下,然后在服务端完成E ...

  7. PostgreSQL index types and index bloating

    warehouse_db=# create table item (item_id integer not null,item_name text,item_price numeric,item_da ...

  8. yii 笔记

    Yii1.1: $_GET 可以表示为 Yii::app()->request->getQuery() $_POST 可以表示为 Yii::app()->request->po ...

  9. Mapper XML Files详解

    扫扫关注"茶爸爸"微信公众号 坚持最初的执着,从不曾有半点懈怠,为优秀而努力,为证明自己而活. Mapper XML Files The true power of MyBatis ...

随机推荐

  1. jquery 字符串转dom对象及对该对象使用选择器查询

    <script> $(document).ready(function () { var htmlStr = '<div id="outerDiv">< ...

  2. gitbook使用

    第一步:安装node.js 官方网址:https://nodejs.org/en/ 运行以下命令,确认是否安装成功 node -v 第二步:安装gitbook npm install -g gitbo ...

  3. [PE结构分析] 7.相对虚拟地址(RVA)和文件偏移间的转换

    RVA是相对虚拟地址(Relative Virtual Address)的缩写.RVA是当PE 文件被装载到内存中后,某个数据位置相对于文件头的偏移量. 例如:导入表的位置和大小可以从PE文件头中IM ...

  4. 设计模式总结篇系列:命令模式(Command)

    在程序设计中,经常会遇到一个对象需要调用另外一个对象的某个方法以达到某种目的,在此场景中,存在两个角色:请求发出者和请求接收者.发出者发出请求,接收者接收请求并进行相应处理.有时候,当需要对请求发出者 ...

  5. 开源VS扩展CodeMaid介绍

         CodeMaid是一个开源的Visual Studio的扩展插件,用于整理与优化代码等.功能类似于商业软件ReSharper,但它是免费的,并且开放源代码.它能帮助你更容易的理解你的代码,支 ...

  6. 面向企业客户的制造业CRM系统的不成熟思考

    CRM就是客户关系管理(Customer Relationship Management),一直一知半解,最近有涉及这方面的需求,所以稍作研究,并思考一些相关问题. CRM是什么? CRM具体如何定义 ...

  7. CSS Hack(转)

    做前端多年,虽然不是经常需要hack,但是我们经常会遇到各浏览器表现不一致的情况.基于此,某些情况我们会极不情愿的使用这个不太友好的方式来达到大家要求的页面表现.我个人是不太推荐使用hack的,要知道 ...

  8. R语言学习笔记:简单的回归分析

    fitbit <- read.csv("fitbit.csv") date     cal   step  dist floor sit inactive walk run2 ...

  9. java多线程系列2-线程控制

    前面的文章已经介绍了有关线程的调度,接下来介绍如何使用方法对线程进行控制 1.线程休眠 public static void sleep(long millis) /* * 线程休眠 * public ...

  10. Java眼中的XML--文件读取--2 应用SAX方式解析XML

    1.获取一个SAXParserFactory的实例.2.通过factory获取SAXParser实例.  3.新建一个包和继承自DefaultHandler的类.  因为SAX遍历方式,比如便利一个节 ...