在集合中使用 "IN" 而不是 "FOR" 循环
Using "IN" instead of "FOR" loop with a collection
我有一个 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;
我尝试上面的方法时出现错误,请帮忙。非常感谢您。
如果您的类型 arrtype 是在数据库级别创建的,您可以使用:
t.name in (select column_value from table(stringArrayIn))
如果你可以在 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 将比循环中的单个更新更有效。
您需要在 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;
/
从第二个例子可以看出,数组元素还是有索引的
我有一个 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;
我尝试上面的方法时出现错误,请帮忙。非常感谢您。
如果您的类型 arrtype 是在数据库级别创建的,您可以使用:
t.name in (select column_value from table(stringArrayIn))
如果你可以在 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 将比循环中的单个更新更有效。
您需要在 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;
/
从第二个例子可以看出,数组元素还是有索引的