update t1 set t1.col1=(select t2.col1 from t2 where t1.col3=t2.col3 and  t2.col2<>t1.col2 )

解决方案 »

  1.   


    update  t1  set  t1.col1=(
    select t2.col1  from  t2  
    where  t1.col3=t2.col3  and    t2.col2<>t1.col2 )
    where exists (select 1 from t2  
    where  t1.col3=t2.col3  and    t2.col2<>t1.col2);   
     
     
     
      

  2.   

    to  bzszp:
    用得着where exists (select 1 from t2  
    where  t1.col3=t2.col3  and    t2.col2<>t1.col2); ??????
      

  3.   

    防止将不符合条件的数据行更新为null.
      

  4.   

    --子查询如果结果没有将返回null值
    update t1 set t1.col1=
    nvl(
    (select t2.col1 from t2 where t1.col3=t2.col3 and  t2.col2<>t1.col2 )
    ,t1.col1
    )
      

  5.   

    update t1 set t1.col1=(select t2.col1 from t2, t1 where t1.col3=t2.col3 and  t2.col2<>t1.col2 )
      

  6.   

    update t1 set t1.col1=t2.col1 from t1,t2 where t1.col3=t2.col3 and  t2.col2<>t1.col2 
    SQL SERVER里没问题,但ORACLE好象不支持。
    ---------------------------------------------------------在sql server中,上述例子可以执行通过,原因是t2.col1 虽然可能返回多个值,
    但是sql server默认取最后一个值为当前值,所以可以执行通过,不会报错!
    但是在oracle中,语法要求比较严格,t2.col1 可以得到多个值,oracle就无从
    知道,应该把哪个值赋给t1.col1,所以,无法执行。
      

  7.   

    也就是说t2.col1 必需从逻辑角度可以判断为唯一值或常量,才可以更新。
      

  8.   

    各位下面两句有区别吗?update t1 set t1.col1=(select t2.col1 from t2, t1 where t1.col3=t2.col3 and  t2.col2<>t1.col2 )
    update t1 set t1.col1=(select t2.col1 from t2 where t1.col3=t2.col3 and  t2.col2<>t1.col2 )