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.

时间: 2024-08-29 00:20:48

Merge compare columns when null的相关文章

SQL基础笔记

Codecademy中Learn SQL, SQL: Table Transformaton和SQL: Analyzing Business Metrics三门课程的笔记,以及补充的附加笔记. Codecademy的课程以SQLite编写,笔记中改成了MySQL语句. I. Learn SQL 1. Manipulation - Create, edit, delete data 1.4 Create 创建数据库或数据库中的表 CREATE TABLE celebs ( id INTEGER,

PostgreSQL index types and index bloating

warehouse_db=# create table item (item_id integer not null,item_name text,item_price numeric,item_data text);CREATE TABLEwarehouse_db=# create index item_idx on item(item_id);CREATE INDEX warehouse_db=# \di item_idx List of relations Schema | Name |

Carbon document

< Getting Started Docs Reference History Contribute Github Introduction The Carbon class is inherited from the PHP DateTime class. <?php namespace Carbon; class Carbon extends \DateTime { // code here } You can see from the code snippet above that t

十步完全理解 SQL

很多程序员视 SQL 为洪水猛兽.SQL 是一种为数不多的声明性语言,它的运行方式完全不同于我们所熟知的命令行语言.面向对象的程序语言.甚至是函数语言(尽管有些人认为 SQL 语言也是一种函数式语言). 我们每天都在写 SQL 并且应用在开源软件 jOOQ 中.于是我想把 SQL 之美介绍给那些仍然对它头疼不已的朋友,所以本文是为了以下读者而特地编写的: 1. 在工作中会用到 SQL 但是对它并不完全了解的人. 2. 能够熟练使用 SQL 但是并不了解其语法逻辑的人. 3. 想要教别人 SQL

通用型动态数组的总结

基本数据结构之-通用型动态数组 动态数组的应用主要是对于长度未知的数组,先开辟一段空间来存储数据,当空间不够时,在开辟两倍的空间来存储数据 和普通数组的区别就是,我们可以不用关心数组的长度的问题,唯一需要关注的就是数据的类型是自定义数据类型还是基本数据类型,但是不论是基本数据类型还是自定义的数据类型,都需要自定义两个函数,这两个函数时遍历(打印)函数和比较函数,因为,在传递的是地址,没法再里面判断是什么类型,只能交给使用者去定义它的想关的函数, 先说基本的结构: 为了适应更多的数据类型,我们存储

SQL SERVER几种数据迁移/导出导入的实践

SQLServer提供了多种数据导出导入的工具和方法,在此,分享我实践的经验(只涉及数据库与Excel.数据库与文本文件.数据库与数据库之间的导出导入). (一)数据库与Excel 方法1: 使用数据库客户端(SSMS)的界面工具.右键选择要导出数据的数据库,选择“任务”——“导出数据”,下图1,按照向导一步一步操作即可.而导入则相反,导入时,SQLServer会默认创建一张新表,字段名也默认跟导入的Excel标题一样,并且会默认字段数据类型等.当然在可以在向导进行修改.需要注意的是如果标题不是

Java编程最差实践常见问题详细说明(2)转

Java编程最差实践常见问题详细说明(2)转 2012-12-13 13:57:20|  分类: JAVA |  标签:java  |举报|字号 订阅 反射使用不当  错误的写法: Java代码   Class beanClass = ... if (beanClass.newInstance() instanceof TestBean) ... 这里的本意是检查beanClass是否是TestBean或是其子类, 但是创建一个类实例可能没那么简单, 首先实例化一个对象会带来一定的消耗, 另外有

一个简单的排序问题

1.首先定义一个接口 package temp.test; public interface IntCompare { public int compare(int a, int b); } 2.分别定义两个接口实现类,一个增序,一个减序 增序: package temp.test; public class IncreaseOrder implements IntCompare{ @Override public int compare(int a, int b) { // TODO Auto

c#将List&lt;T&gt;转换成DataSet

/// <summary> /// List<T> 转换成DataSet /// </summary> /// <typeparam name="T">对象</typeparam> /// <param name="list">集合</param> /// <returns>DataSet</returns> public static DataSet Con