更新查询的问题

时间:2011-09-05 08:01:44

标签: sql oracle plsql

我有一个查询

UPDATE Table_1 SET Col1='Y' WHERE ROWID IN (
    select ROWID from (
        SELECT BUS_ID,
               row_number() over (partition by BUS_ID order by BUS_ID) dupe_count,
               rowid
        from Table_1
        WHERE col2 <> 1
          AND col3 <> 1
        order by dbms_random.value 
    ) ft
    where ft.dupe_count = 1
      AND ROWNUM <= 1000 
);

仅更新表Table_1中的1000行。

但如果我写

 UPDATE Table_1 SET Col1='Y' WHERE ROWID IN (
     select ROWID from (
         SELECT BUS_ID,
                row_number() over (partition by BUS_ID order by BUS_ID) dupe_count,
                rowid
         from Table_1
         WHERE col2 <> 1
           AND col3 <> 1
         order by dbms_random.value 
     ) ft
     where ft.dupe_count = 1
       and Table_1.BUS_ID = ft.BUS_ID
       AND ROWNUM <= 1000 
);

它更新表的所有行而不管RoWNUM <= 1000,即如果我添加

Table_1.BUS_ID = ft.BUS_ID

然后它会更新满足col2<> 1 AND col3<> 1 and ft.dupe_count=1的所有行。 该表具有以下结构:

BUS_ID | col1 | col2 | col3
     1 |      |   0  | 0
     2 |      |   0  | 0
     1 |      |   0  | 0
     3 |      |   1  | 1.

知道为什么会这样。请帮助。

1 个答案:

答案 0 :(得分:4)

尼拉吉,

为每个表评估普通子查询。针对每一行评估相关子查询。并且您已在第二个更新语句中将子查询与行Table_1.BUS_ID = ft.BUS_ID相关联。如果它评估每一行,那么它将始终满足ROWNUM <= 1000谓词。

的问候,
罗布。