2012-03-22 54 views
0

我有一个模式,它看起来像这样从DB2行中的一个表中的列迁移数据的其他表

table 1 
------- 
ID TYPE VALUE 
============== 
1 A 10 
1 B 200 
2 A 20 
2 B 500 


table 2 
------------- 
ID typeA typeB 
============== 
1 10 200 
2 20 500 

我的迁移脚本是

update table2 set typeA = (select t1.value from table1 t1 
where t1.ID = table2.ID and t1.type = 'A'), 
typeB = (select t1.value from table1 t1 where t1.ID = table2.ID and t1.type='B'); 

现在,当有两个工作正常每个id的类型,并且如果缺少一个类型的行,则会失败,并出现sql错误代码407。我尝试使用IFNULL,COALESCE,但似乎没有工作。我知道这是一个必须多次解决的问题,但无法在任何地方直接得到答案。

回答

1

COALESCE应该为你工作,这是否给你一个错误?

update table2 t2 
set typeA = COALESCE((select t1.value 
         from table1 t1 
         where t1.ID = t2.ID 
         and t1.type = 'A'), 0), 
    typeB = COALESCE((select t1.value 
         from table1 t1 
         where t1.ID = t2.ID 
         and t1.type='B'), 0); 
相关问题