从批量集合中选择

时间:2014-10-31 16:08:46

标签: oracle plsql cursor bulk-collect

是否可以从批量收藏中进行选择?

这些方面的东西:

DECLARE
  CURSOR customer_cur IS
    SELECT CustomerId,
          CustomerName
    FROM   Customers
    WHERE  CustomerAreaCode = '576';

  TYPE customer_table IS TABLE OF customer_cur%ROWTYPE;
  my_customers  customer_table; 
BEGIN

  OPEN customer_cur;

  FETCH customer_cur
  BULK COLLECT INTO my_customers;

  -- This is what I would like to do
  SELECT CustomerName
    FROM my_customers
   WHERE CustomerId IN (1, 2, 3); 

END;

我似乎无法从the my_customers表中选择。

2 个答案:

答案 0 :(得分:3)

是的,你可以。声明自己的模式级类型如下:

create or replace rec_customer_cur
as
object (
    customerid       integer, -- change to the actual type of customers.customerid
    customername     varchar2(100) -- change to the actual type of customers.customername
);
/

create or replace type customer_table
as
table of rec_customer_cur;
/

然后,在您的PLSQL代码中,您可以声明

CURSOR customer_cur IS
SELECT new rec_customer_cur(CustomerId, CustomerName)
FROM   Customers
WHERE  CustomerAreaCode = '576';

...然后使用......

SELECT CustomerName
INTO whatever
FROM table(my_customers)
WHERE CustomerId IN (1, 2, 3); 

这是因为在SQL上下文中可以使用模式级类型

答案 1 :(得分:0)

如果您还要显示select返回的数据集,请使用REF CURSOR作为OUT参数。

SELECT ...FROM TABLESQL语句,需要STATIC TABLE NAME作为database object。它会抛出一个错误,因为集合名称实际上不是一个数据库表作为对象。

要返回数据集,请使用SYS_REFCURSOR作为OUT参数。

open cur as select....