输入对象数组作为输入,并从存储过程返回数组作为输出

时间:2019-10-07 12:20:43

标签: oracle stored-procedures plsql oracle12c

我有一个要求,我需要将嵌套的对象数组作为存储过程的输入,从表中以不同日期范围的组获取数据,并返回所有日期范围的数据作为输出。我是PL / Sql的新手,因此请在此处请求指导。

我已经阅读了很多有关实现此目标的文章,但是我只了解存储过程和pl / sql的基本概念,但是无法实现这种情况。

1 个答案:

答案 0 :(得分:1)

Oracle设置

创建要输入的集合:

def funcion(n):
    # every row has n - 1 slots that can be filled with 1 as one must be 0
    # (the diagonal) - we must pick exactly two slots
    slots_with_one = (n - 1) * (n - 2) // 2
    # now that we picked those in all other rows we must put a one on those
    # columns exactly - if not that column won't have two ones, so we're done
    return slots_with_one

print(funcion(3))
print(funcion(4))
print(funcion(5))

创建要输出的集合:

CREATE TYPE intlist AS TABLE OF int;

创建一些测试数据:

CREATE TYPE daterange AS OBJECT(
  start_date DATE,
  end_date   DATE
);
/
CREATE TYPE daterangelist AS TABLE OF daterange;

过程

CREATE TABLE test_data ( id, start_date, end_date ) AS SELECT 1, DATE '2019-01-01', DATE '2019-01-02' FROM DUAL UNION ALL SELECT 2, DATE '2019-01-02', DATE '2019-01-03' FROM DUAL UNION ALL SELECT 3, DATE '2019-01-03', DATE '2019-01-04' FROM DUAL UNION ALL SELECT 4, DATE '2019-01-04', DATE '2019-01-05' FROM DUAL; 表中进行选择的简单过程,它将把日期范围收集到输出参数中,其中该行的test_data是输入数组的id

MEMBER OF

测试

CREATE PROCEDURE test_proc (
  i_ints  IN  intlist,
  o_dates OUT daterangelist
)
IS
BEGIN
  SELECT daterange( start_date, end_date )
  BULK COLLECT INTO o_dates
  FROM   test_data
  WHERE  id MEMBER OF i_ints;
END;
/

输出:

01-JAN-19 - 02-JAN-19
03-JAN-19 - 04-JAN-19
04-JAN-19 - 05-JAN-19

db <>提琴here