使用过滤器值从表类型中获取特定行

时间:2019-01-03 18:45:19

标签: oracle plsql oracle-sqldeveloper plsqldeveloper

我的程序如下:

cursor my_cursor is 
select  first_column, second_column, third_column from table_name;

TYPE my_cursor_type is TABLE OF my_cursor_type%ROWTYPE INDEX BY BINARY INTEGER; 
my_cur  my_cursor_type;

TYPE table_type IS TABLE OF table_name%ROW_TYPE  INDEX BY BINARY INTEGER; 
table_obj  table_type; 

begin 

open my_cursor; 
loop
fetch my_cursor bulk collect 
into my_cur limit 5000;
exit when my_cursor%notfound; 
for i in 1 .. my_cur.count loop   
       table_obj(i).first_column  := my_cur(i).first_column; 
       table_obj(i).second_column := my_cur(i).second_column;   
       table_obj(i).third_column := my_cur(i).third_column;  
     end loop;
  end loop;
  Close my_cursor;

……

现在这些代码之后有table_obj,它具有100000条记录。并且此表对象具有first_column,second_column,third_column。

我在table_obj中搜索一条记录的third_column。我知道first_column,second_column,并且我在table_obj中搜索third_column。我必须从table_obj行中获取行。此搜索行具有我的first_column,second_column值。 我从这一行中获得third_column_value。如何使用plsql从table_obj行中获取特定行?

1 个答案:

答案 0 :(得分:0)

这可以通过将前两列转换为单个索引来完成。为此,使用分隔符串联最有效。分隔符应该是前两列均未使用的字符。

DECLARE
   CURSOR my_cursor
   IS
      SELECT first_column, second_column, third_column FROM table_name;

   TYPE my_cursor_type IS TABLE OF my_cursor_type%ROWTYPE
      INDEX BY BINARY_INTEGER;
   my_cur           my_cursor_type;

   TYPE table_type IS TABLE OF table_name%ROWTYPE
      INDEX BY BINARY_INTEGER;
   table_obj        table_type;

   l_composite_idx  VARCHAR2( 61 );
   l_test_idx_1     VARCHAR2( 30 ) := 'Test1';
   l_test_idx_2     VARCHAR2( 30 ) := 'Success';
BEGIN
   OPEN my_cursor;

   LOOP
      FETCH my_cursor BULK COLLECT INTO my_cur LIMIT 5000;

      FOR i IN 1 .. my_cur.COUNT
      LOOP
         l_composite_idx                             := my_cur( i ).first_column || '&' || my_cur( i ).second_column;

         table_obj( l_composite_idx ).first_column   := my_cur( i ).first_column;
         table_obj( l_composite_idx ).second_column  := my_cur( i ).second_column;
         table_obj( l_composite_idx ).third_column   := my_cur( i ).third_column;
      END LOOP;

      EXIT WHEN my_cursor%NOTFOUND;
   END LOOP;

   CLOSE my_cursor;

   -- You access a row in my_cur like this:
   DBMS_OUTPUT.put_line( 'Result: ' || table_obj( l_test_idx_1 || '&' || l_test_idx_2 ) );
END;

希望这会有所帮助!