2014-09-23 40 views
0

我在想,如果下面的代码是一个很好的做法制作联接使用的rowid在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没有,所以在这种情况下它不会匹配。请让我知道你的想法。

+3

你究竟想要完成什么?有很多原因可能会改变'rowid' - 重新插入一行只是其中的一个。你的目标是什么? “我想知道下面的代码是否是一种好的做法,是一种很好的做法? – 2014-09-23 17:54:06

+0

使用rowid进行连接并不是一种好的做法。请放桌子结构。 – AndreDuarte 2014-09-23 19:55:52

+0

只要你不保存它,以后使用rowid没有任何问题。由于使用rowid进行查找非常有效,因此可以考虑使用它的场景。在这种情况下,这是一个很好的做法,我不能说。 – 2014-09-23 20:53:32

回答

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 
);