`
luzl
  • 浏览: 564399 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

DB2 Update/Delete With Jion

    博客分类:
  • DB2
阅读更多
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 dummy
where not exists (select 1 from mytable where mycolumn='12345')
分享到:
评论

相关推荐

    SQL语句生成及分析器(中文绿色)

    1、支持绝大部分数据库,包括 大型数据库Oracle,Sybase(包括SQL AnyWhere),DB2,MS_SQL 中型数据库MS_Access,MySQL 桌面型数据库Paradox,DBF系列数据库... 10.4 简单SQL查询语句转换为Delete,Update,Insert语句

    sql语句生成与分析器.rar

    11.4 简单SQL查询语句转换为Delete,Update,Insert语句 11.5 复制为字符串(支持对Java、C#、Delphi、VB、PowerBuilder开发语言的支持) 11.6 灵活的拖放功能 11.7 在线版本更新 11.8 查询结果输出为SQL脚本...

    经典全面的SQL语句大全

     DML—数据操纵语言(SELECT,DELETE,UPDATE,INSERT)  DCL—数据控制语言(GRANT,REVOKE,COMMIT,ROLLBACK)  首先,简要介绍基础语句:  1、说明:创建数据库 CREATE DATABASE database-name  2、说明:删除...

    SQL语法大全

    sql="update 数据表 set 字段1=值1,字段2=值2 …… 字段n=值n where 条件表达式" (3) 删除数据记录: sql="delete from 数据表 where 条件表达式" sql="delete from 数据表" (将数据表所有记录删除) (4) 添加...

    oracle学习文档 笔记 全面 深刻 详细 通俗易懂 doc word格式 清晰 连接字符串

    例如:INSERT(插入)、UPDATE(修改)、DELETE(删除)语句  数据查询语言 (Data Query Language, DQL) 是SQL语言中,负责进行数据查询而不会对数据本身进行修改的语句,这是最基本的SQL语句。例如:SELECT(查询...

    超实用sql语句

    DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。 7、说明:添加主键: Alter table tabname add primary key(col) 说明:删除主键: Alter table tabname drop primary key(col) 8、说明...

    经典SQL语句大全

    DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。 7、说明:添加主键: Alter table tabname add primary key(col) 说明:删除主键: Alter table tabname drop primary key(col) 8、说明...

    数据库操作语句大全(sql)

    DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。 7、说明:添加主键: Alter table tabname add primary key(col) 说明:删除主键: Alter table tabname drop primary key(col) 8、说明...

    sql经典语句一部分

    DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。 7、说明:添加主键: Alter table tabname add primary key(col) 说明:删除主键: Alter table tabname drop primary key(col) 8、说明...

    程序员的SQL金典6-8

     7.2 内连接(INNER JOIN)  7.3 不等值连接  7.4 交叉连接  7.5 自连接  7.6 外部连接  7.6.1 左外部连接  7.6.2 右外部连接  7.6.3 全外部连接 第8章 子查询  8.1 子查询入门  8.1.1 单值子查询  8.1.2...

    程序员的SQL金典7-8

     7.2 内连接(INNER JOIN)  7.3 不等值连接  7.4 交叉连接  7.5 自连接  7.6 外部连接  7.6.1 左外部连接  7.6.2 右外部连接  7.6.3 全外部连接 第8章 子查询  8.1 子查询入门  8.1.1 单值子查询  8.1.2...

    程序员的SQL金典3-8

     7.2 内连接(INNER JOIN)  7.3 不等值连接  7.4 交叉连接  7.5 自连接  7.6 外部连接  7.6.1 左外部连接  7.6.2 右外部连接  7.6.3 全外部连接 第8章 子查询  8.1 子查询入门  8.1.1 单值子查询  8.1.2...

    程序员的SQL金典4-8

     7.2 内连接(INNER JOIN)  7.3 不等值连接  7.4 交叉连接  7.5 自连接  7.6 外部连接  7.6.1 左外部连接  7.6.2 右外部连接  7.6.3 全外部连接 第8章 子查询  8.1 子查询入门  8.1.1 单值子查询  8.1.2...

    程序员的SQL金典.rar

     7.2 内连接(INNER JOIN)  7.3 不等值连接  7.4 交叉连接  7.5 自连接  7.6 外部连接  7.6.1 左外部连接  7.6.2 右外部连接  7.6.3 全外部连接 第8章 子查询  8.1 子查询入门  8.1.1 单值子查询  8.1.2...

Global site tag (gtag.js) - Google Analytics