更新Oracle中的行以引用其他表行

时间:2012-10-10 12:12:35

标签: oracle10g

嗨,大家好我正在尝试更新这样的行:

declare
   cursor c1 is select core_cust_id, ADDRESS_LINE1, ADDRESS_LINE2, ADDRESS_LINE3,CITY_CODE,
   STATE_CODE from crmuser.address where corp_id is not null and 
ADDRESSCATEGORY='Mailing';
   begin
   for i in c1 loop
   update  crmuser.tradefinance set ADDR_1=i.ADDRESS_LINE1,
   ADDR_2=i.ADDRESS_LINE2, ADDR_3=i.ADDRESS_LINE3, CITY_CODE=i.CITY_CODE,
   STATE_CODE=i.STATE_CODE       
   where i.core_cust_id=(select core_cust_id from crmuser.tradefinance where corp_id is not null);
   end loop; 
   end;

我收到错误:

ORA-01427: single-row subquery returns more than one row
ORA-06512: at line 7

执行时

select core_cust_id, ADDRESS_LINE1, ADDRESS_LINE2, ADDRESS_LINE3,CITY_CODE,
   STATE_CODE from crmuser.address where corp_id is not null and 
ADDRESSCATEGORY='Mailing';

我获得了485条记录,我希望与Core_cust_id匹配。当我执行:

select core_cust_id from crmuser.tradefinance where corp_id is not null;

我再次获得485 core_cust ID以匹配循环中的其他Core_cust_ids ...为什么我的查询返回多个记录?我应该纠正什么?我正在使用Oracle 10g

1 个答案:

答案 0 :(得分:1)

试试这个

declare
   cursor c1 is select core_cust_id, ADDRESS_LINE1, ADDRESS_LINE2, ADDRESS_LINE3,CITY_CODE,
   STATE_CODE from crmuser.address where corp_id is not null and 
ADDRESSCATEGORY='Mailing';
   begin
   for i in c1 loop
   update  crmuser.tradefinance set ADDR_1=i.ADDRESS_LINE1,
   ADDR_2=i.ADDRESS_LINE2, ADDR_3=i.ADDRESS_LINE3, CITY_CODE=i.CITY_CODE,
   STATE_CODE=i.STATE_CODE       
   where i.core_cust_id=crmuser.tradefinance.core_cust_id 
   and crmuser.corp_id is not null;
   end loop; 
   end;