PL SQL映射结果集为多个记录

时间:2015-10-22 09:12:22

标签: sql oracle plsql

如果我们有3个表,例如A,B和C以及像

这样的游标
FOR i IN (
     SELECT * 
       FROM A 
       JOIN B ON(...)
       JOIN C ON(...)
) LOOP
--is there an easy way to map every row to 3 records(A%rowtype, B%rowtype and C%rowtype)?
END LOOP;

1 个答案:

答案 0 :(得分:2)

请注意,按ROWID提取记录的速度要快于索引。但是通过以下技巧,你将获得微小的性能损失(与简单的FOR LOOP相比),因为没有任何东西是免费的。

DECLARE
  l_rec_a table_A%ROWTYPE;
  l_rec_b table_B%ROWTYPE;
BEGIN

  FOR i IN (SELECT a.ROWID first
                 , b.ROWID second
                 FROM table_A AS a
                 JOIN table_B AS b on a.id = b.id)
  LOOP
    SELECT * INTO l_rec_a FROM table_A WHERE ROWID = i.first;
    SELECT * INTO l_rec_b FROM table_B WHERE ROWID = i.second;

   /* do something */

  END LOOP;

END;