DB2 insert or update 解决方案(使用merge into 解决一条记录,或者多表关系问题) |
您所在的位置:网站首页 › db2update › DB2 insert or update 解决方案(使用merge into 解决一条记录,或者多表关系问题) |
merge into table1 t1
using (select column1 ,column2 from table2) t2
on t1.column1 = t2.column1
when matched then update set t1.column2 = t2.column2 when not matched then insert values(column1,column2)
以上是把 table2中的数据插入或者更新到table1中。
但是对于开发人员程序来说,我们是一条一条记录来处理的,判断这条数据是insert 还是 update
对于DB2的处理办法如下:
merge into table1 t1 using ( select 程序中的数据1 as column1, 程序中的数据2 as column2 from SYSIBM.SYSDUMMY1 ) t2
//保证t2 一定是有一条数据的,就是你程序中的那条数据,SYSIBM.SYSDUMMY1 是db2 专用的,当然这里你可以用别的办法来处理,比如用select count(1) from 任何一个table都可以。 on a.column1= b.column1 when matched then update set t1.column2 = t2.column2 when not matched then insert values(column1,column2) |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |