使用“IN”而不是“FOR”循环与集合

时间:2016-02-18 10:19:55

标签: oracle collections plsql sql-in

我有一个plsql程序,它接受一组数据并更新一堆记录,我可以使用for循环执行此操作。我真的可以使用一些帮助找出没有循环的事情。

套餐规格和正文:

create or replace PACKAGE ARRAY_EXAMPLE AS
  type arrtype is table of test_table.name%TYPE index by pls_integer;

  PROCEDURE PROCESS_ARRAY( stringArrayIn IN arrtype
                         , p_city           varchar2
                         , p_phone          number);
END;
/

create or replace PACKAGE BODY ARRAY_EXAMPLE AS

  PROCEDURE PROCESS_ARRAY( stringArrayIn IN arrtype
                         , p_city           varchar2
                         , p_phone          number) IS   
  BEGIN
   FOR i IN 1..stringArrayIn.Count
   LOOP
      update test_table t
         set t.city = p_city 
       where t.name = stringArrayIn(i)
         and t.phone = p_phone;
   END LOOP;
  END;
END;
/

我想拥有的内容:

create or replace PACKAGE BODY ARRAY_EXAMPLE AS

  PROCEDURE PROCESS_ARRAY( stringArrayIn IN arrtype
                           , p_city           varchar2
                           , p_phone          number) IS   
  BEGIN
     update test_table t
        set t.city = p_city
      where t.phone = p_phone
        and t.name in (stringArrayIn);
  END;
END;

我在尝试上述操作时遇到错误,请帮忙。非常感谢你。

3 个答案:

答案 0 :(得分:2)

您需要在SQL而不是PL / SQL中定义集合:

create type arrtype is table of VARCHAR2(100);

create or replace PACKAGE BODY ARRAY_EXAMPLE AS
  PROCEDURE PROCESS_ARRAY(
    stringArrayIn IN arrtype
  , p_city        IN varchar2
  , p_phone       IN number
  )
  IS 
  BEGIN
    update test_table t
    set    t.city  = p_city
    where  t.phone = p_phone
    and    t.name  MEMBER OF stringArrayIn
  END;
END;
/

<强>更新

在实例化时初始化一个数组:

DECLARE
  t_names ARRTYPE := t_names( 'Alice', 'Bob', 'Charlie' );
BEGIN
  ARRAY_EXAMPLE.PROCESS_ARRAY(
    t_names,
    'New York City',
    '555-2368'
  );
END;
/

稍后填充数组:

DECLARE
  t_names ARRTYPE;
BEGIN
  t_names := ARRTYPE();
  t_names.EXTEND(3);
  t_names(1) := 'Alice';
  t_names(2) := 'Bob';
  t_names(3) := 'Charlie';

  ARRAY_EXAMPLE.PROCESS_ARRAY(
    t_names,
    'New York City',
    '555-2368'
  );
END;
/

从第二个示例中可以看出,数组元素仍然被编入索引。

答案 1 :(得分:1)

如果在数据库级别创建类型 arrtype ,则可以使用:

t.name in (select column_value from table(stringArrayIn))

答案 2 :(得分:1)

如果您可以在SQL级别定义类型,则可以执行以下操作:

create type  SQL_arrtype is table of varchar2(50) /* for example, it is the type of your name field */

CREATE OR REPLACE PACKAGE ARRAY_EXAMPLE AS

    PROCEDURE PROCESS_ARRAY(
                            stringArrayIn    IN SQL_arrtype,
                            p_city              VARCHAR2,
                            p_phone             NUMBER
                           );
END;
CREATE OR REPLACE PACKAGE BODY ARRAY_EXAMPLE AS
    PROCEDURE PROCESS_ARRAY(
                            stringArrayIn    IN SQL_arrtype,
                            p_city              VARCHAR2,
                            p_phone             NUMBER
                           ) IS
    BEGIN
        forall i in 1 .. stringArrayIn.COUNT
        UPDATE test_table t
               SET t.city    = p_city
             WHERE     t.name = stringArrayIn(i)
                   AND t.phone = p_phone;
    END;
END;

FORALL将比循环中的单个更新更有效。