Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

oracle - how to append values to a plsql table

I have to get ID values from 3 different queries (Q1, Q2 and Q3 below) and append them to a plsql table so that I can use it later on using table(). But I am unable to "append" to the table. Can you please let me know how to go about this? I am in Oracle 12G. I dont want to union all the queries as in my production scenario, I need to pull from 15 different queries and dont want to write a huge one. And everytime, I "put" into this plsql table, the earlier data gets wiped and doesnt see to append. Any idea how to achieve this using 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;
/

question from:https://stackoverflow.com/questions/65948411/how-to-append-values-to-a-plsql-table

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

So there's no BULK COLLECT APPEND or anything, but you can combine multiple nested tables with MULTISET UNION. So you could populate multiple nested tables and then combine them. I'm not sure what the performance would be compared to a single UNION SQL query, but I expect it would be much better than anything working in a loop.

So you'd do something like this:

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...