在PLSQL中使用rowid进行连接

时间:2014-09-23 17:30:49

标签: oracle plsql

我想知道以下代码是否是一个好习惯

CURSOR c_price_hist_parent IS 
  select tran_type, reason, event,
         unit_cost, unit_retail, selling_unit_retail,
         selling_uom, multi_units, multi_unit_retail,
         multi_selling_uom 
    from price_hist
   where rowid = ( SELECT row_id 
                     from ( SELECT rowid row_id
                              FROM price_hist 
                             WHERE item = l_item_parent
                               and tran_type in (4,8)
                               and loc = l_location
                               and ACTION_DATE <= l_create_date
                             order by action_date desc 
                           )
                    where rownum = 1 
                  ); 

如果我们删除一行然后插入同一行,则所有列保持相同但rowid不相同,因此在这种情况下它将不匹配。请让我知道你的想法。

1 个答案:

答案 0 :(得分:0)

如前所述,使用rownum可能很棘手,因为插入或删除可能同时发生,这可能会改变rownums。 如果我正确理解您的查询,您只是尝试使用最新的action_date获取记录。为什么不这样做:

CURSOR c_price_hist_parent IS 
  select sub.tran_type, sub.reason, sub.event,
         sub.unit_cost, sub.unit_retail, sub.selling_unit_retail,
         sub.selling_uom, sub.multi_units, sub.multi_unit_retail,
         sub.multi_selling_uom 
    from (
     select *
       from price_hist
      where item = l_item_parent
        and tran_type in (4,8)
        and loc = l_location
        and ACTION_DATE <= l_create_date
      order by action_date desc
    ) sub
  where rownum = 1
);