2

下面是我创建的一个函数,用于接受一个varchar2项目数组并返回该记录的内部 pk,该记录是NUMBER每个记录的一个。我正在努力获得正确的语法,以将类型数组传递VARCHAR_ARRAY给游标中的简单 sql 查询并返回类型变量NUMBER_ARRAY。错误在第 8,42 行,即FROM table_name WHERE column_name IN VARCHAR_ARRAY传递给函数。在我学习 plsql 时请帮我解决这个错误。

  create or replace TYPE VARCHAR_ARRAY AS VARRAY(1000000) OF VARCHAR2(1000);
   /

  create or replace TYPE NUMBER_ARRAY AS VARRAY(1000000) OF NUMBER;
   /

  create or replace Function GET_PRODUCT_ID_ARR(V_PRODUCT_NUM_ARR IN VARCHAR_ARRAY)
  RETURN NUMBER_ARRAY 
  IS
     product_id_list number_array := number_array(); 
     CURSOR c1
     IS 
     SELECT cat_map_id 
     FROM mn_cat_map WHERE product_num IN (V_PRODUCT_NUM_ARR) and catalog_type = 'INT';
  v_output NUMBER;   
  BEGIN
      OPEN c1; 
      LOOP
          fetch c1 into product_id_list;
          EXIT WHEN c1%notfound;
          product_id_list.extend;
          product_id_list(product_id_list.count)  := v_output;
         dbms_output.put_line('Product ('||v_output ||'):'||product_id_list(v_output));
      END LOOP;
  Close c1;
  RETURN product_id_list;
  END;
  /
4

2 回答 2

1

@William 说的是真的。VARRAY(1000000)不推荐。您可以就地创建一种table. 但是,如果我按照您所做的操作,您的代码中似乎存在一些错误。请看下面如何做到这一点。

表准备:

create table mn_cat_map(cat_map_id number,
                        product_num varchar2(1000),
                        catalog_type varchar2(10));
/
INSERT INTO T541682.MN_CAT_MAP (CAT_MAP_ID, PRODUCT_NUM, CATALOG_TYPE)
     VALUES (10, 'A123', 'INT');

INSERT INTO T541682.MN_CAT_MAP (CAT_MAP_ID, PRODUCT_NUM, CATALOG_TYPE)
     VALUES (2, 'B121', '2Wheer');

INSERT INTO T541682.MN_CAT_MAP (CAT_MAP_ID, PRODUCT_NUM, CATALOG_TYPE)
     VALUES (3, 'C645', '4Wheer');

COMMIT;

create or replace TYPE VARCHAR_ARRAY AS VARRAY(1000000) OF VARCHAR2(1000);
/
create or replace TYPE NUMBER_ARRAY AS VARRAY(1000000) OF NUMBER;
/

代码:内联阅读解释性注释

CREATE OR REPLACE FUNCTION GET_PRODUCT_ID_ARR (V_PRODUCT_NUM_ARR  VARCHAR_ARRAY)
   RETURN NUMBER_ARRAY
IS
   product_id_list   number_array := number_array ();

   CURSOR c1(tbl_list VARCHAR_ARRAY)
   IS
      SELECT cat_map_id
        FROM mn_cat_map
       WHERE product_num  in (select column_value from table(tbl_list)) ---Checking if the item exists in the table with passed collection 
       AND catalog_type = 'INT';

   v_output     NUMBER:= 0;
BEGIN

    --not opening cursor and am not looking for processing any records.
   --OPEN c1(V_PRODUCT_NUM_ARR);

   --passing the input varray to the cursor.
   for i in c1(V_PRODUCT_NUM_ARR)
   loop

    v_output:=v_output + 1;

    product_id_list.extend;

    product_id_list(product_id_list.COUNT):= i.cat_map_id;

     DBMS_OUTPUT.put_line('Product (' || v_output || '):' ||product_id_list(product_id_list.COUNT));


   end loop;

   RETURN product_id_list;
END;
/

执行:

SQL> select GET_PRODUCT_ID_ARR(VARCHAR_ARRAY('A123','B121','C645')) COl1 from dual;

COL1
--------------------------------------------------------------------------------
NUMBER_ARRAY(10)

Product (1):10
于 2016-12-08T14:08:53.023 回答
1

有两个问题:

  1. 你必须投到varray表:

    CURSOR c1
    IS 
    SELECT cat_map_id 
    FROM mn_cat_map 
    WHERE product_num IN (select column_value from table(V_PRODUCT_NUM_ARR))
      and catalog_type = 'INT';
    
  2. bulk collect之后添加fetch

    LOOP
      fetch c1 bulk collect into product_id_list limit 100;
      EXIT WHEN c1%notfound;
      product_id_list.extend;
      product_id_list(product_id_list.count)  := v_output;
      dbms_output.put_line('Product ('||v_output ||'):'||product_id_list(v_output));
      END LOOP;
    

如果你写limit 100,每个循环将放入 100 条记录product_id_list。您可以省略limit子句,在这种情况下,您将一次获取所有记录。

编辑
如何查看结果:

declare 
  myarray varchar_array; 
  my_num_array number_array;
begin 
  myarray := varchar_array(); 
  myarray.extend(2);
  myarray(1) := '151043'; 
  myarray(2) := '2895'; 
  my_num_array := GET_PRODUCT_ID_ARR(myarray);
  for i in 1 .. my_num_array.count loop
    dbms_output.put_line(my_num_array(i)); 
  end loop; 
end;
/
于 2016-12-08T07:44:49.333 回答