SQL 行列表的存储过程数据类型

SQL Stored Procedure data type for list of rows

我可以使用什么数据类型来存储 SELECT 查询找到的所有行?

CREATE OR REPLACE PROCEDURE handleFailedCalls(xNumber in varchar(10)) AS
result {DATA TYPE I WANT};
BEGIN
   select * into result
   from CALLS c1
   where c1.status = 'fail'
END
/

使用BULK COLLECT,例子:

DECLARE
TYPE emp_typ IS TABLE OF employees%ROWTYPE INDEX BY PLS_INTEGER;
all_employees emp_typ;
BEGIN
SELECT * BULK COLLECT INTO all_employees FROM employees;

A SELECT ... BULK COLLECT INTO statement can return multiple rows. You must set up collection variables to hold the results. You can declare associative arrays or nested tables that grow as needed to hold the entire result set.

一种方法是使用 OUT 类型的 REFCURSOR 变量。

CREATE OR REPLACE PROCEDURE handleFailedCalls(xNumber in varchar2,
p_result OUT SYS_REFCURSOR
) AS
BEGIN
   OPEN p_result FOR select * from CALLS c1
   where c1.status = 'fail'
END
/

此外,使用 VARCHAR2 而不是 VARCHAR。它应该没有大小,因为带有大小的过程参数将无法编译。

可以调用该过程将光标接收到局部 ref cursor 变量中。

DECLARE
res_cur SYS_REFCURSOR;
BEGIN
    handleFailedCalls('Xnumber1', res_cur );
END;
/