如何将值附加到 plsql table
how to append values to a plsql table
我必须从 3 个不同的查询(下面的 Q1、Q2 和 Q3)中获取 ID 值并将它们附加到 plsql table 以便我以后可以在使用 table() 时使用它.但是我无法“附加”到 table。你能告诉我该怎么做吗?我在 Oracle 12G 中。我不想像在我的生产场景中那样合并所有查询,我需要从 15 个不同的查询中提取并且不想编写一个巨大的查询。每次,我“放入”此 plsql table,较早的数据都会被擦除并且看不到追加。知道如何使用 plsql table 来实现吗?
CREATE OR replace PACKAGE pkg AS
TYPE customer_id_table_type IS TABLE OF all_customers.customer_id%TYPE;
PROCEDURE process_customers;
END pkg;
/
CREATE OR replace PACKAGE BODY pkg AS
PROCEDURE process_customers IS
customer_id_table customer_id_table_type := customer_id_table_type();
BEGIN
-- Q1
SELECT customer_id BULK COLLECT
INTO customer_id_table
FROM old_customers cust
WHERE cust.last_update_date BETWEEN SYSDATE - 100 AND SYSDATE;
-- Q2
SELECT customer_id BULK COLLECT
INTO customer_id_table
FROM new_customers cust
WHERE cust.last_update_date BETWEEN SYSDATE - 100 AND SYSDATE;
-- Q3
SELECT customer_id BULK COLLECT
INTO customer_id_table
FROM archive_customers cust
WHERE cust.last_update_date BETWEEN SYSDATE - 100 AND SYSDATE;
FOR rec IN
(
SELECT customer_info
FROM all_customers acc
WHERE customer_id IN (SELECT * FROM TABLE(customer_id_table)))
LOOP
dbms_output.put_line('customer info: '|| rec.customer_info);
END LOOP;
END process_customers;
END pkg;
/
所以没有 BULK COLLECT APPEND
或任何东西,但是您可以将多个嵌套表格与 MULTISET UNION
结合起来。因此,您可以填充多个嵌套表,然后将它们组合起来。我不确定与单个 UNION
SQL 查询相比性能如何,但我希望它会比循环中的任何工作要好得多。
所以你会这样做:
SELECT customer_id BULK COLLECT
INTO customer_id_table_1
FROM old_customers cust
WHERE cust.last_update_date BETWEEN SYSDATE - 100 AND SYSDATE;
SELECT customer_id BULK COLLECT
INTO customer_id_table_2
FROM new_customers cust
WHERE cust.last_update_date BETWEEN SYSDATE - 100 AND SYSDATE;
customer_id_table_1 := customer_id_table_1 MULTISET UNION customer_id_table_2;
我必须从 3 个不同的查询(下面的 Q1、Q2 和 Q3)中获取 ID 值并将它们附加到 plsql table 以便我以后可以在使用 table() 时使用它.但是我无法“附加”到 table。你能告诉我该怎么做吗?我在 Oracle 12G 中。我不想像在我的生产场景中那样合并所有查询,我需要从 15 个不同的查询中提取并且不想编写一个巨大的查询。每次,我“放入”此 plsql table,较早的数据都会被擦除并且看不到追加。知道如何使用 plsql table 来实现吗?
CREATE OR replace PACKAGE pkg AS
TYPE customer_id_table_type IS TABLE OF all_customers.customer_id%TYPE;
PROCEDURE process_customers;
END pkg;
/
CREATE OR replace PACKAGE BODY pkg AS
PROCEDURE process_customers IS
customer_id_table customer_id_table_type := customer_id_table_type();
BEGIN
-- Q1
SELECT customer_id BULK COLLECT
INTO customer_id_table
FROM old_customers cust
WHERE cust.last_update_date BETWEEN SYSDATE - 100 AND SYSDATE;
-- Q2
SELECT customer_id BULK COLLECT
INTO customer_id_table
FROM new_customers cust
WHERE cust.last_update_date BETWEEN SYSDATE - 100 AND SYSDATE;
-- Q3
SELECT customer_id BULK COLLECT
INTO customer_id_table
FROM archive_customers cust
WHERE cust.last_update_date BETWEEN SYSDATE - 100 AND SYSDATE;
FOR rec IN
(
SELECT customer_info
FROM all_customers acc
WHERE customer_id IN (SELECT * FROM TABLE(customer_id_table)))
LOOP
dbms_output.put_line('customer info: '|| rec.customer_info);
END LOOP;
END process_customers;
END pkg;
/
所以没有 BULK COLLECT APPEND
或任何东西,但是您可以将多个嵌套表格与 MULTISET UNION
结合起来。因此,您可以填充多个嵌套表,然后将它们组合起来。我不确定与单个 UNION
SQL 查询相比性能如何,但我希望它会比循环中的任何工作要好得多。
所以你会这样做:
SELECT customer_id BULK COLLECT
INTO customer_id_table_1
FROM old_customers cust
WHERE cust.last_update_date BETWEEN SYSDATE - 100 AND SYSDATE;
SELECT customer_id BULK COLLECT
INTO customer_id_table_2
FROM new_customers cust
WHERE cust.last_update_date BETWEEN SYSDATE - 100 AND SYSDATE;
customer_id_table_1 := customer_id_table_1 MULTISET UNION customer_id_table_2;