我有两个过程。当过程1调用第二个过程时,它将返回一个表作为out参数。有时,过程2可能被调用多次。问题是如何将从过程2获得的所有内容合并(或追加)到一张表中?
表类型是用户定义的:
CREATE OR REPLACE TYPE t_record AS OBJECT
(
i NUMBER,
city_name VARCHAR2(100)
);
create or replace type t_table as table of t_record;
我了解MULTISET UNION ALL
,但是似乎无法使用它,因为我不知道在过程1中将调用过程2多少次。
我该怎么办?也许我所做的一切都是错误的,并且有更好的方法来做到这一点。我很高兴听到任何建议。
FOR @hotfix
我简化了过程1的代码。它实际上在循环内和循环后调用过程2。
FOR insurance_program IN insurance_programs LOOP
IF insurance_program.level_of_service = 'additional' THEN
Procedure2(client_id, 2, insurance_program.program_id, avaliable_cities);
END IF;
END LOOP;
Procedure2(client_id, 1, 1, avaliable_cities);
答案 0 :(得分:1)
使用MULTISET UNION ALL
将其附加到您要返回的数组中:
CREATE PROCEDURE Procedure1(
client_id IN NUMBER,
available_cities OUT t_table
)
IS
program_available_cities t_table;
-- Dummy cursor with 3 rows.
CURSOR insurance_programs IS
SELECT LEVEL AS program_id,
'additional' AS level_of_service
FROM DUAL
CONNECT BY LEVEL <= 3;
BEGIN
available_cities := t_table();
FOR insurance_program IN insurance_programs LOOP
IF insurance_program.level_of_service = 'additional' THEN
Procedure2( client_id, 2, insurance_program.program_id, program_available_cities );
available_cities := available_cities MULTISET UNION ALL program_available_cities;
END IF;
END LOOP;
END;
/
示例:
CREATE OR REPLACE TYPE t_record AS OBJECT
(
i NUMBER,
city_name VARCHAR2(100)
);
create or replace type t_table as table of t_record;
-- Dummy procedure to output 4 cities every time it is called
CREATE PROCEDURE Procedure2(
client_id IN NUMBER,
variable1 IN NUMBER,
program_id IN NUMBER,
available_cities OUT t_table
)
IS
BEGIN
available_cities := t_table();
available_cities.EXTEND(4);
FOR i IN 1 .. 4 LOOP
available_cities(i) := t_record(
4 * program_id + i - 4,
'City' || TO_CHAR( 4 * program_id + i - 4 )
);
END LOOP;
END;
/
DECLARE
available_cities t_table;
BEGIN
Procedure1( 1, available_cities );
FOR i in 1 .. available_cities.COUNT LOOP
DBMS_OUTPUT.PUT_LINE( available_cities(i).i || ': ' || available_cities(i).city_name );
END LOOP;
END;
/
输出:
1: City1
2: City2
3: City3
4: City4
5: City5
6: City6
7: City7
8: City8
9: City9
10: City10
11: City11
12: City12
db <>提琴here