as400 update语句 问题

1、************************************************
我新建了两张物理表,tab_1,tab_2,其中它们的数据分别为
tab_1
------------------------------------
bm   mc          value1     value2
01   01编码        0          0
02   02编码        0          0
03   03编码        0          0
------------------------------------
tab_2
------------------------------------
bm   mc          value1
01   01编码        100         
------------------------------------
2、************************************************
然后我使用update语句
update tab_1 a set value1 = (select value1 from tab_2 b where a.bm=b.bm);
3、************************************************
可是最后查询结果却是这样
select * from tab_1;
tab_1
------------------------------------
bm   mc          value1     value2
01   01编码        100         0
02   02编码         -          0
03   03编码         -          0
------------------------------------
为02,03编码的value1值变成空值了。
select * from tab_1 where value1 is null;
tab_1
------------------------------------
bm   mc          value1     value2
02   02编码         -          0
03   03编码         -          0
------------------------------------
参与10

7同行回答

Xiao QingXiao Qing系统工程师浪潮商用机器有限公司
update tab_1 a set value1 = (select value1 from tab_2 b where a.bm=b.bm) 这条SQL语句不管它该不该更新所有记录,但实际上它更新了 TAB_1这张表中的每一条记录,更糟糕的是TAB_2表中的 bm 字段的内容即使没有出现在TAB_1表中,这条SQL语句也将会把 TAB_1相对应的 value1字...显示全部

update tab_1 a set value1 = (select value1 from tab_2 b where a.bm=b.bm) 这条SQL语句不管它该不该更新所有记录,但实际上它更新了 TAB_1这张表中的每一条记录,更糟糕的是TAB_2表中的 bm 字段的内容即使没有出现在TAB_1表中,这条SQL语句也将会把 TAB_1相对应的 value1字段置为null,这是绝对不准许的,因此要进行更有选择性的更新以避免错误的发生。

有两种方法可以解决这个问题:

Method 1:
update twtlib.tab_1 a
set value1=(select value1 from twtlib.tab_2 b where a.bm=b.bm)
where bm in (select bm from twtlib.tab_2);

Method 2:
update twtlib.tab_1 a set value1 = (select value1 from twtlib.tab_2 b where a.bm=b.bm)
where exists (select 1 from twtlib.tab_2 b where a.bm=b.bm);

执行结果如下图所示:

然后查看tab_1的内容,没有被误改为null的记录了,符合要求。

仅供参考。

收起
系统集成 · 2021-04-15
浏览1126
zjadolfzjadolf软件开发工程师联创亚信科技(南京)有限公司
这样写就行了update tab_1 a set value1 = (select value1 from tab_2 b where a.bm=b.bm)where exists (select 1 from tab_2 b where a.bm=b.bm);db2的多表关联的update语法和oracle 一样的;显示全部
这样写就行了
update tab_1 a set value1 = (select value1 from tab_2 b where a.bm=b.bm)
where exists (select 1 from tab_2 b where a.bm=b.bm);
db2的多表关联的update语法和oracle 一样的;收起
互联网服务 · 2011-04-18
浏览1129
db2_wldb2_wl系统工程师weall
谢谢4楼的回复,但是我没看明白啊。显示全部
谢谢4楼的回复,但是我没看明白啊。收起
政府机关 · 2011-03-22
浏览1059
db2_wldb2_wl系统工程师weall
感觉2楼的解释挺不错的,我使用update语句时,只是要修改这一列的值:update tab_1 a set value1 = (select value1 from tab_2 b where a.bm=b.bm); 并且这一列是同tab_2中编码相同的,可是其它不相同的没有加入限制条件。...显示全部
感觉2楼的解释挺不错的,我使用update语句时,只是要修改这一列的值:update tab_1 a set value1 = (select value1 from tab_2 b where a.bm=b.bm); 并且这一列是同tab_2中编码相同的,可是其它不相同的没有加入限制条件。收起
政府机关 · 2011-03-22
浏览1051
anliguoanliguo数据库管理员NEW COM
idkjyold#/db2aix/iprocess$db2expln -d sample -q "update tab_1 a set value1 = (select value1 from tab_2 b where a.bm=b.bm)" -g  -tDB2 Universal Database Version 9.5, 5622-044 (c) Copyright IBM Corp. 1991, 2007Licensed Material - Program Prop...显示全部
idkjyold#/db2aix/iprocess$db2expln -d sample -q "update tab_1 a set value1 = (select value1 from tab_2 b where a.bm=b.bm)" -g  -t

DB2 Universal Database Version 9.5, 5622-044 (c) Copyright IBM Corp. 1991, 2007
Licensed Material - Program Property of IBM
IBM DB2 Universal Database SQL and XQUERY Explain Tool

DB2 Universal Database Version 9.5, 5622-044 (c) Copyright IBM Corp. 1991, 2007
Licensed Material - Program Property of IBM
IBM DB2 Universal Database SQL and XQUERY Explain Tool

******************** DYNAMIC ***************************************

==================== STATEMENT ==========================================

        Isolation Level          = Cursor Stability
        Blocking                 = Block Unambiguous Cursors
        Query Optimization Class = 5

        Partition Parallel       = No
        Intra-Partition Parallel = No

        SQL Path                 = "SYSIBM", "SYSFUN", "SYSPROC", "SYSIBMADM",
                                   "IPROCESS"


Statement:
  
  update tab_1 a set value1 =
     (select value1
     from tab_2 b
     where a.bm=b.bm)


Section Code Page = 1208

Estimated Cost = 37.868073
Estimated Cardinality = 3.000000

Access Table Name = IPROCESS.TAB_1  ID = 3,65
|  #Columns = 1
|  Relation Scan
|  |  Prefetch: Eligible
|  Lock Intents
|  |  Table: Intent Exclusive
|  |  Row  : Exclusive
Nested Loop Join
|  Piped Inner
|  Access Table Name = IPROCESS.TAB_2  ID = 3,66
|  |  #Columns = 1
|  |  Evaluate Block/Data Predicates Before Locking Committed Row
|  |  Relation Scan
|  |  |  Prefetch: Eligible
|  |  Lock Intents
|  |  |  Table: Intent Share
|  |  |  Row  : Next Key Share
|  |  Sargable Predicate(s)
|  |  |  #Predicates = 1
Update:  Table Name = IPROCESS.TAB_1  ID = 3,65

End of section


Optimizer Plan:

                 Rows   
               Operator
                 (ID)   
                 Cost   
                       
                  3   
               UPDATE  
                ( 2)   
               37.8681
              /       \
          3             3     
       NLJOIN        Table:   
        ( 3)         IPROCESS
       15.1715       TAB_1   
      /       \
     3       0.04   
  TBSCAN    TBSCAN  
   ( 4)      ( 5)   
  7.58444   7.58316
    |         |     
    3         1     
Table:    Table:   
IPROCESS  IPROCESS
TAB_1     TAB_2   

我的推断应该是对的:-)收起
互联网服务 · 2011-03-21
浏览1070
db2_wldb2_wl系统工程师weall
问题解决了,在后面加上where条件就好了update tab_1 a set value1 = (select value1 from tab_2 b where a.bm=b.bm)where a.a1 in(select c.a1 from t1 c);显示全部
问题解决了,在后面加上where条件就好了
update tab_1 a set value1 = (select value1 from tab_2 b where a.bm=b.bm)
where a.a1 in(select c.a1 from t1 c);收起
政府机关 · 2011-03-21
浏览1063
anliguoanliguo数据库管理员NEW COM
select value1 from tab_2 b where a.bm=b.bm对啊 这个正常啊,你这个做连接的时候 tab_2 本来只有一行啊,你这相当于一个左连接啊。你既然要Update tab_1 他就要扫描你所有的tab_1的行,所以要选择三行,但是里面的查询你tab_2没有 那没办法只好为NULL了。这是我的理解。...显示全部
select value1 from tab_2 b where a.bm=b.bm

对啊 这个正常啊,你这个做连接的时候 tab_2 本来只有一行啊,你这相当于一个左连接啊。
你既然要Update tab_1 他就要扫描你所有的tab_1的行,所以要选择三行,但是里面的查询你tab_2没有 那没办法只好为NULL了。这是我的理解。收起
互联网服务 · 2011-03-21
浏览1052

提问者

db2_wl
系统工程师weall
擅长领域: 数据库

相关问题

相关资料

相关文章

问题状态

  • 发布时间:2011-03-21
  • 关注会员:2 人
  • 问题浏览:6188
  • 最近回答:2021-04-15
  • X社区推广