FORALL 循环 - 来自关联数组的 select 语句

FORALL loop - select statement from associative array

我有一个函数,它接受关联数组作为参数。函数 - 在 insert/update 之前 - 需要检查 table 中是否存在记录,所以我需要遍历数组。

因为 FORALL 不允许 SELECT 语句,正确的方法是什么?

功能(只是我需要修复的部分 - 这是一个例子!!):

 type t_name is table of MySchema.Orders.NAME%type index by pls_integer;
 type t_year is table of MySchema.Orders.YEAR%type index by pls_integer;
 type t_month is table of MySchema.Orders.MONTH%type index by pls_integer;

 FUNCTION Check_records (name_in IN  t_name, year_in IN  t_year, month_in IN  t_month) RETURN INTEGER
 IS
  
 record_exists INTEGER;
 
 BEGIN
    
      FORALL i in name_in.FIRST..name_in.LAST
      SELECT COUNT(*) INTO record_exists 
      FROM MySchema.Orders@link_to_table
      WHERE name= name_in(i)
      AND year= year_in(i)
      AND month= month_in(i);
     
      return record_exist
         
 END Check_records;

最简单的方法就是使用 for 循环

DECLARE
  l_total_records pls_integer;
  l_count         pls_integer;
BEGIN
  l_total_records := 0;

  FOR i in name_in.FIRST..name_in.LAST
  LOOP
    SELECT COUNT(*) 
      INTO l_count 
      FROM MySchema.Orders@link_to_table
     WHERE name= name_in(i)
       AND year= year_in(i)
       AND month= month_in(i);

    l_total_count := l_total_count + l_count;
  END LOOP;
END;

如果您正在查询本地 table 并且您可以使用单个集合(即具有 nameyearmonth 的对象类型的集合字段),你可以用 MEMBER OF 函数做一些更优雅的事情,但我不确定它是否适用于数据库 link 并且目前没有可供测试的。