在循环中调用 Table 函数以获取数据转储

Calling a Table Function in a loop to get a data dump

我需要在以下场景中使用 table 函数。我在一个 table 中有一个 person_ids 的列表,在另一个 table 中有一个 learning_path_ids 的列表。所以我想知道是否有员工完成了任何学习路径。为此,我有一个名为 Employees_Learning_Chk_f(person_I'd, learning_path_id) 的函数,其中 returns 10。因此,通过传递 person_idlearning_path_id,我可以查明该人是否已完成该学习路径。我需要为组织中的所有员工找到这个以获取数据转储。问题是我们有 9000 名员工和 250 条学习路径。因此,我必须通过传递所有 person_id : learning_path_id 组合来循环 运行 上述函数以获得数据转储的最终输出。

请注意,人员 ID 和学习路径之间的关系在这里被丢弃,因为我们还需要历史信息(不仅是与当前关系相关的详细信息,因为员工可能与更早的学习路径)。

以下是我的做法。

--Defining types required.
CREATE TYPE tf_row AS OBJECT (
  person_id           NUMBER,
  learning_path_id    NUMBER,
  completion_status   NUMBER,
);
/

CREATE TYPE tf__tab IS TABLE OF tf_row;
/

-- Build the table function.
CREATE OR REPLACE FUNCTION get_completion_status (person_id IN NUMBER) RETURN tf_tab AS
  l_tab  tf_tab := tf_tab();
  status_flag number;
  --Getting all learning path IDs using a cursor.
  cursor c1 is
     SELECT distinct learning_path_id 
     FROM learning_paths;

BEGIN

   FOR learnings in c1
   LOOP
   --Calling an existing function which returns 1 if the employee
   --holding this person_id has completed the learning path related to 
   --this learning_path_id.
   select Employees_Learning_Chk_f(person_id, learnings.learning_path_id) 
   into status_flag
   From Dual;

   --If the learning path has been completed, the information is 
   --inserted to in memory table.
   If dual = 1 then
   l_tab.extend;
   l_tab(l_tab.last) := tf_row(person_id, learnings.learning_path_id, status_flag);
   end if;

   END LOOP;

   RETURN l_tab;
END;

现在我可以通过如下传递 person_id 来调用上述函数...

select from table (get_completion_status(123456));

输出将是这样的:

但我需要以类似的方式获取所有 9000 名员工的输出。这怎么可能?是否可以使用 PIPELINED 功能?如果是,你能解释一下吗?

注: 关于这个,我什至不能临时在数据库中创建任何 table。

我正在使用 Oracle-SQL 连接到 Oracle 11g 数据库的开发人员。

您可以使用笛卡尔积(交叉联接)在单个查询中执行此操作:

select p.person_id, l.learning_path_id, Employees_Learning_Chk_f(p.person_id, l.learning_path_id) as status
from per_all_people_f p
cross join ota_learning_paths l;

如果您可以描述 Employees_Learning_Chk_f 的作用,也可以通过在查询中添加另一个 table 来消除它。