更新查询投掷错误

时间:2013-07-21 19:23:20

标签: sql oracle oracle10g

我需要表T1和T2。我想从T1更新T2中的第四个coloumn。查询是

update t1 
set   t1.price=(select price from (select t1.price 
                            from t2 
                            where t1.customer=t2.customer and t1.sku=t2.sku and 
                                  t1.weekno= t2.weekno) where rownum=1)

但它抛出错误无效标识符t1.weekno。我尝试了其他几种方法,但每次遇到同样的问题。如果你能帮助我,我将不胜感激。谢谢

P.S:我们正在处理5亿条记录。

2 个答案:

答案 0 :(得分:1)

您的查询存在的问题是您在相关子查询的select语句中引用了t1.price。有几种方法可以解决这个问题。

一种选择是使用MERGE

merge
into t1
using   (
        select  t1.customer as updatedCustomer, t2.sku updatedsku, t2.weekno updatedweekno, t2.price updatedPrice
        from    t1
            join    t2
                on      t1.customer = t2.customer and t1.sku=t2.sku and 
                              t1.weekno= t2.weekno
        )
on      (customer = updatedCustomer and sku = updatedsku and weekno = updatedweekno)
when matched then
update
set     price = updatedPrice;

或者,您可以更新相关子查询以使用t2.price:

update t1 
set price = (select t2.price 
             from t2 
             where t1.customer=t2.customer and t1.sku=t2.sku and 
                              t1.weekno= t2.weekno and rownum = 1)

答案 1 :(得分:0)

您需要在子查询中添加t1到from子句:

select t1.price 
                            from t1,t2
                            where t1.customer=t2.customer and t1.sku=t2.sku and 
                                  t1.weekno= t2.weekno