update cp_kpi_info_fy t4 set t4.resperson = (select t5.account from security_ultra_user t5, tb_tmp_wei t6 where t6.name = t5.account and t4.capescode = t6.code)

解决方案 »

  1.   

    它怎么报t4.capescode无效的错误。我前面定义的t4,在(select)里不能用吗?
      

  2.   

    列名写错了吧,语法没问题, LZ这句SQL会update cp_kpi_info_fy 中所有的数据哦
      

  3.   

    不会更新所有的行吧?我只是想把t4里.capescode和t6里.code相同的行里,t4的resperson和t6里的name是否对应,不对应的就更新一下。因为t4的resperson是英文名,t6里的name是中文名。所以要用t6里的name和t5里的user_name对应,找到t5里的account(英文名)。然后再和t4的resperson对照更新update cp_kpi_info_fy t4 set t4.resperson = (select t5.account from security_ultra_user t5, tb_tmp_wei t6 where t6.name = t5.user_name and t4.capescode = t6.code)这样写对吗?
      

  4.   

    现在他报"single-row subquery returns more than one row"的错误。t4是指标和对应负责人的表,t5是英文名和中文名的对应表,t6也是指标和对应负责人的表(从excel里导入的)需要写个sql自动检查t6里的指标和对应负责人是否和t4中的一致。因为t4的数值较老,可能不对
      

  5.   

    你后面没有加where条件,所以会更新所有行. 应该加上where条件
    UPDATE cp_kpi_info_fy t4
       SET t4.resperson =
                        (SELECT t5.ACCOUNT
                           FROM security_ultra_user t5, tb_tmp_wei t6
                          WHERE t6.NAME = t5.user_name AND t4.capescode = t6.code and rownum<=1)
     WHERE t4.capescode IN (SELECT t6.code
                              FROM security_ultra_user t5, tb_tmp_wei t6
                             WHERE t6.NAME = t5.user_name)
      

  6.   

    为什么有2个where呢?rownum<1 这句有什么用呢?按理说t4和t6没有重复指标,不应该返回很多行相同的吧?
      

  7.   

    你报错的信息是select t5.account from security_ultra_user t5, tb_tmp_wei t6 where t6.name = t5.account and t4.capescode = t6.code这里返回了多个值,oracle不知道更新哪个值才行。你先查询看看数据的准确性,正常情况你这样更新是可以的。
      

  8.   

    update cp_kpi_info_fy t4 set t4.resperson = 
    (select t5.account from security_ultra_user t5, tb_tmp_wei t6 where t6.name = t5.account and t4.capescode = t6.code)
    例如t4.capescode=1那么t6.code=1的情况下,t6.name有多个,导致返回了多个account。
      

  9.   

    为什么有2个where呢?
    这两个where的用处不同,第一个用于select,是要找出对应的ACCOUNT.第二个用于update,说明只有这些数据要更新,如果不加后面这个where,就会把所有数据都更新一次,在select中没有找到值的就会被更新为null
    rownum<1 这句有什么用呢?按理说t4和t6没有重复指标,不应该返回很多行相同的吧?  
    rownum<1是让Oracle不报"single-row subquery returns more than one row",因为Oracle怀疑这句select会找出多行,仅仅是怀疑而已.用rownum<=1就是告诉Oracle,这里只有一条记录.
      

  10.   

    楼上的你这样如果返回的是多个account,你就只取第一个。完全是为了避免"single-row subquery returns more than one row"错误,如果子查询出来没有返回多行,这个更新语句是没问题的。不需要在加where条件。