您的位置:程序门 -> db2 -> 数据管理



至急 db2 update多表关联问题


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


至急 db2 update多表关联问题
发表于:2007-12-28 09:33:26 楼主
看过前面一些帖子,写了一个,

update   db2gkdp.f2dd_trn_sch   a   set   a.effective_flg   =   (select   distinct   b.effective_flg   from   db2gkdp.f2dd_trn_sch   a,   db2gkdp.f2dd_trn_mode   b   where   a.ship_fr_factory_cd   =   b.ship_fr_factory_cd   and   a.ship_to_factory_cd   =   b.ship_to_factory_cd   and   a.trn_mode_cd   =   b.trn_mode_cd)

但是,当检索出的数据   〉2时,

错误发生:
sql0811n     the   result   of   a   scalar   fullselect,   select   into   statement,   or   values   into   statement   is   more   than   one   row.

试问,这种方法是不是只适用于当检索为1条时。
如果是多条的时候,如何写才能更新多条。


发表于:2007-12-28 12:42:521楼 得分:0
update   tab1   set   column1   =   (select   column2   from   tab2   where   条件   fetch   first   1   rows   only)
需要更新成的值必须是一条记录
如果是多条更新,则必须指定对应关系
比如:
tab1
a             b
--------------
a1             b1
a2             b2
a3             b3

tab2
a           c
---------------
a1         c1
a2         c2
a3         c3

如果想把tab2里的c列更新为tab1里的b列
则:update   tab2   t2   set   c   =   (select   b   from   tab1   t1   where   t1.a   =   t2.a   fetch   first   1   rows   only)


发表于:2007-12-28 13:22:092楼 得分:0
按照你的这种写法(可更新多条)
update   tab2   t2   set   c   =   (select       b       from       tab1       t1       where       t1.a       =       t2.a       fetch       first       1       rows       only)  

执行后:

sql0901n     the   sql   statement   failed   because   of   a   non-severe   system   error.   subsequent   sql   statements   can   be   processed.     (reason   "can   not   enforced   maxcard   >   1      

还是不可以更新多条。
还是我的理解有问题。
发表于:2007-12-28 15:43:203楼 得分:0
db2   =>   select   *   from   a;

a           b                       c
-----   -----------   -----------
aa                             1                       1
aa                             1                       1
bb                             2                       2
bb                             2                       2
bb                             2                       2

db2   =>   select   *   from   b;

h                       u
-----------   -----
                    1   aa
                    2   bb
                    2   cc

db2   =>   update       a           set       c       =(select       b.h             from           b       where       a.b=b.h   fetch   first   1   rows   only);
db20000i     sql   命令成功完成。

测试好像没有问题。

发表于:2008-01-07 21:34:564楼 得分:0
检索出的数据   〉2时,a.effective_flg到底要被update为哪个数据,不确定,肯定不对了
发表于:2008-01-10 11:43:415楼 得分:0
可以更新多条的,因为更新时两个表是有对应条件的。除非每个对应条件有多条数据时才会报错,这种情况就是设计逻辑错误了。

这是测试过程:
一、db2   select   *   from   a  
id                           name                                           value                                          

-----------   --------------------   --------------------  

                          1   n1                                                   v1                                                  

                          2   n2                                                   v2                                                  


2   条记录已选择。  


二、db2   select   *   from   b  

id                           name                                           value                                          

-----------   --------------------   --------------------  

                        11   n1                                                   x                                                  

                        12   n1                                                   y                                                  

                        13   n2                                                   z                                                  


3   条记录已选择。
 
三、db2   update   b   set   b.value   =   (select   a.value   from   a   where   a.name   =   b.name)  
db20000i   sql命令成功完成。  


四、db2   select   *   from   b  
id                           name                                           value                                          

-----------   --------------------   --------------------  

                        11   n1                                                   v1                                                  

                        12   n1                                                   v1                                                  

                        13   n2                                                   v2                                                  


3   条记录已选择。


快速检索

最新资讯
热门点击