六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 88|回复: 0

DB2 Update/Delete With Jion

[复制链接]

升级  60.33%

117

主题

117

主题

117

主题

举人

Rank: 3Rank: 3

积分
381
 楼主| 发表于 2013-1-13 18:36:14 | 显示全部楼层 |阅读模式
When you need to update one table based on the other table -
it is called "correlated update".  You have to repeat the same "where"
logic 2 times.

update maintab m set (m.fname, m.lname) =  (select u.fname,u.lname from updatetab u where m.id=u.id) where exists  (select null from updatetab u where m.id=u.id);

or variation: use "in" expression:

update maintab m set (m.fname, m.lname) = (select u.fname,u.lname from updatetab u where m.id=u.id) where m.id in  (select u.id from updatetab);

or delete expression:
delete from maintab m where exists  (select null from updatetab u where m.id=u.id);  

Note: If you don't include the 2nd "where" clause - then  ALL rows in the
main table will be updated (putting NULLs in all rows which are not part of the join)
======================
Please note: the following Sybase syntax DOES NOT WORK in DB2:
                update maintab                set m.fname=u.fname, m.lname=u.lname                from  maintab m, updates u                where m.id=u.id
======================

How to insert a row - but only if the row with the value of a file doesn't exist:
insert into mytable (mycolumn) select '12345' from table (values 1) as dummywhere not exists (select 1 from mytable where mycolumn='12345')
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表