BULK COLLECT INTO UNION 查询到 table 个对象

BULK COLLECT INTO a UNION query into a table of objects

我如何收集到 table 个对象中,其中包含一个联合的查询生成的值,如下所示

Select customer_name
from customer
where customer_id = 'xxx'
BULK COLLECT INTO customer_obj
UNION
Select customer_name
from customer
where customer_name like '%adam%'

上面的约束条件完全是编出来的

bulk collect 子句紧跟在(第一个)select 子句之后,在(第一个)from 子句之前。你放错地方了。

不清楚您为什么使用 union(尽管它本身 而不是 会导致错误)。也许作为一个 意外的 结果,你会得到一个 distinct 名字的列表,因为那是 union 所做的(相对于 union all).

除此之外,正如已经在评论中指出的那样,您不需要 union - 您需要 where 子句中的 or。但即使您以这种方式修改查询,您仍然必须将 bulk collect 移动到适当的位置。

另一种选择是将您的 UNION 放入内联视图中。例如,

SELECT          cust.customer_name 
BULK            COLLECT 
INTO            customer_obj
FROM (
  SELECT        customer_name
  FROM          customer
  WHERE         customer_id = 'xxx'
  UNION
  SELECT        customer_name
  FROM          customer
  WHERE         customer_name LIKE '%adam%'
) cust